Is it possible to auto reboot for 5 loops through mint? - linux

I am currently using the following command to run reboot
sudo shutdown -r now
however, I would need to run it for 5 loops before and after executing some other programs. Was wondering if it is possible to do it in MINT environment?

First a disclaimer: I haven't tried this because I don't want to reboot my machine right now...
Anyway, the idea is to make a script that can track it's iteration progress to a file as #david-c-rankin suggested. This bash script could look like this (I did test this):
#!/bin/sh
ITERATIONS="5"
TRACKING_FILE="/path/to/bootloop.txt"
touch "$TRACKING_FILE"
N=$(cat "$TRACKING_FILE" | wc -c)
if [ "$N" -lt "$ITERATIONS" ]; then
printf "." >> "$TRACKING_FILE"
echo "rebooting (iteration $N)"
# TODO: this is where you put the reboot command
# and anything you want to run before rebooting each time
else
rm "$TRACKING_FILE"
# TODO: other commands to resume anything required
fi
Then add a call to this script somewhere where it will be run on boot. eg. cron (#reboot) or systemd. Don't forget to remove it from a startup/boot command when you're finished or next time you reboot, it will reboot N times.
Not sure exactly how you are planning on using it, but the general workflow would look like:
save script to /path/to/reboot_five_times.sh
add script to run on boot (cron, etc.)
do stuff (manually or in a script)
call the script
computer reboots 5 times
anything in the second TODO section of the script is then run
go back to step 3, or if finished remove from cron/systemd so it won't reboot when you don't want it to.

First create a text document wherever you want,I created one on Desktop,
Then use this file as a physical counter and write a daemon file to run things at startup
For example:
#!/bin/sh
var=$(cat a.txt)
echo "$var"
if [ "$var" != 5 ]
then
var=$((var+1))
echo "$var" > a.txt
echo "restart here"
sudo shutdown -r now
else
echo "stop restart"
echo 0 > a.txt
fi
Hope this helps

I found a way to create a file at startup for my reboot script. I incorporated it with the answers provided by swalladge and also shubh. Thank you so much!
#!/bin/bash
#testing making a startup application
echo "
[Desktop Entry]
Type=Application
Exec=notify-send success
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Name[en_CA]=This is a Test
Name=This is a Test
Comment[en_CA]=
Comment=" > ~/.config/autostart/test.desktop

I create a /etc/rc.local file to execute user3089519's script, and this works for my case. (And for bootloop.txt, I put it here: /usr/local/share/bootloop.txt )
First: sudo nano /etc/rc.local
Then edit this:
#!/bin/bash
#TODO: things you want to execute when system boot.
/path/to/reboot_five_times.sh
exit 0
Then it works.
Don't forget edit /etc/rc.local and remove /path/to/reboot_five_times.sh when you done reboot cycling.

Related

Can't write to file with script

I have written a script that is supposed to run some commands, reboot my ubuntu server and then after the server boots up, resume operation, and removes the entry from the bash file.
I have been trying for hours to figure out why my script won't write to .bashrc (or any file for that matter). Testing the commands individually works just fine. However, when run through my script nothing is written to the file. At this point I am stumped and would really like a fresh pair of eyes to help as I am sure the reason is silly and probably something I am missing. This would be my first script so sorry if this ends up being a stupid question. I put the "sudo reboot command in a comment so I won't have to reboot each time.
The script's name is test.sh and is run from ~/ . I hope this is clear and I didn't miss anything.
#!/bin/bash
echo "Script initiating"
#condition for script to run after reboot, created later on
if [ ! -f /var/run/bootflag ]; then
echo "First run"
script="bash ~/test.sh"
#this will add the script in the bash file so it will be ran on next boot
echo "$script" >> ~/.bashrc
echo "bash entry added"
#creating flag file to check if this is a second run
sudo touch /var/run/bootflag
echo "Flag created"
echo "Rebooting..."
#sudo reboot
else
echo "resuming script..."
echo "cleaning up..."
#remove the bash entry by replacing it with a space
sed -i '/bash/d' ~/.bashrc
echo "bash entry removed"
#remove the boot flag
sudo rm -f /var/run/bootflag
echo "bootflag removed"
echo "running commands post-reboot"
#commands here
echo "script exiting"
fi
I finally figured it out.
Because I was running the script with sudo, the script would take the relative path ~/.bashrc for the root user. The script was working fine all along, I was just looking at my user's .bashrc and was expecting things to appear, when all along the root user's .bashrc file was being written in.
I used sudo inside the script for all elevated commands and run the script normally without sudo and there it is, working as expected.
As I thought, this was beyond silly after all. At least I learned something today.
Thank you all for taking the time to reply and help, I really appreciate it.

Script runs fully when ran manually but misses commands when ran from another script

I have 2 scripts that I'm testing to automate starting services on my server however they behave weirdly.
The first script is
#!/bin/sh
screen -dmS Test_Screen
sleep 1
sudo sh cd.sh
echo "finished"
Which runs perfectly however the script it runs does not and is as follows
#!/bin/sh
screen -S Test_Screen -X stuff "cd /home/Test"
sleep 1
screen -S Test_Screen -X eval "stuff \015"
sleep 1
echo "Complete"
The second script will run perfect if I run it from command line and will CD into the directory within the screen. However, if it runs from the first script it Will Not CD into the correct directory within the screen, but it will still print "Complete".
I'm Using CENTOS 6.7 and the latest version of GNU screen
Any Ideas?
This seems to be a problem with session nesting.
In your first script you create a session named Test_Screen.
In your second script the -S parameter tells screen to create a session of the same name. This might cause screen to exit and not cd into the correct directory.
You could move the cd command in front of the sudo sh cd.sh and remove those screen calls from the second script leaving only
stuff \015
echo "Complete"
Using the correct screenflags should also work.
#!/bin/sh
screen -dr Test_Screen -X stuff "cd /home/Test"
sleep 1
screen -dr Test_Screen -X eval "stuff \015"
sleep 1
echo "Complete"
For a more modern alternative to screen, have a look at tmux.
Ok so this turned out really weird. After posting i tried a couple of things on a centos 6.7 hyper V test environment and got the exact same issue. However, later in the day we ended up changing service provider and upgrading to centos 7 in the process. I am not sure why but since the update the script now runs perfectly and i was able to actually merge the two scripts into one in order to make it more efficient. If anyone knows why the update fixed it feel free to let me know.

Why this Debian-Linux autostart netcat script won't autostart?

I placed a link to my scripts in the rc.local to autostart it on linux debian boot. It starts and then stops at the while loop. It's a netcat script that listens permantently on port 4001.
echo "Start"
while read -r line
do
#some stuff to do
done < <(nc -l -p 4001)
When I start this script as root with command ./myscript it works 100% correctly. Need nc (netcat) root level access or something else?
EDIT:
rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
/etc/samba/SQLScripts
exit 0
rc.local starts my script "SQLScripts"
SQLScripts
#! /bin/sh
# The following part always gets executed.
echo "Starting SQL Scripts" >> /var/log/SQLScriptsStart
/etc/samba/PLCCheck >> /var/log/PLCCheck &
"SQLScripts" starts "PLCCheck" (for example only one)
PLCCheck
#!/bin/bash
echo "before SLEEP" >> /var/log/PLCCheck
sleep 5
echo "after SLEEP" >> /var/log/PLCCheck
echo "vor While" >> /var/log/PLCCheck
while read -r line
do
echo "in While" >> /var/log/PLCCheck
done < <(netcat -u -l -p 6001)
In an rc script you have root level access by default. What does "it stops at the while loop" mean? It quits after a while, or so? I guess you need to run your loop in the background in order to achieve functionality usual in autostart scripts:
echo "Starting"
( while read -r line
do
#some stuff to do
done << (nc -l -p 4001) ) &
echo "Started with pid $( jobs -p )"
I have tested yersterday approximatly the same things, and I have discover that you can bypass the system and execute your netcat script with the following crontask. :
(every minute, but you can ajust that as you want.)
* * * * * /home/kali/script-netcat.sh // working for me
#reboot /home/kali/script-netcat.sh // this is blocked by the system.
According to me, I think that by default debian (and maybe others linux distrib) block every script that try to execute a netcat command.

Linux Ubuntu: Script works in terminal, but not .sh

Issue Summary: My script works as it should when typed into the terminal, however, it does not work correctly when executed in terminal from a .sh file, why is this?
Script:
echo World of Clucky - Frisnuk "\033]0;Frisnuk - World of Clucky\a"
#! /usr/bin/env bash
BINDIR="$(dirname "$(readlink -fn "$0")")"
cd "$BINDIR"
while true
do
source /home/clucky/MinecraftServers/Frisnuk/serverconfig/config.sh
#Start Server
java -Xms2000M -Xmx2000M -jar $serverjar.jar nogui
if [ "`date +%w%H`" = "001" ]
then
#Delete map files for The End
rm -R /Frisnuk_the_end
echo "End has been successfully reloaded"
echo "[`date +%D\ %T`] End Reloaded" >> /home/clucky/MinecraftServers/Frisnuk/EndRestart.txt
#Change Item of The Week
weekofyear=`date +%y\-%U`
s=$(<serverconfig/ItemofTheWeek/item$weekofyear.txt)
set -- $s
itemoftheweekid=$2
itemoftheweekprice=$3
xmlstarlet edit -L -u "/scs-shop/itemStack[#type='double']" -v $itemoftheweekid /plugins/ShowCaseStandalone/ffs-storage/ffss_cac8480951254352116d5255e795006252d404d8
xmlstarlet edit -L -u "/scs-shop/price[#type='double']" -v $itemoftheweekprice /plugins/ShowCaseStandalone/ffs-storage/ffss_cac8480951254352116d5255e795006252d404d8
fi
echo "If you want to stop the restart and shut the server off instead, please press Ctrl+C at this time"
for i in 5 4 3 2 1
do
echo "$i..."
sleep 1
done
echo "Restarting Server"
clear
done
Instead of working and running the server, it just says this:
World of Clucky - Frisnuk
/home/clucky/MinecraftServers/Frisnuk/craftminecraft.sh: 7: /home/clucky/MinecraftServers/Frisnuk/craftminecraft.sh: source: not found
Error: Unable to access jarfile .jar
If you want to stop the restart and shut the server off instead, please press Ctrl+C at this time
5...
4...
3...
2...
1...
I am going to take a shower shortly, but I will be returning either later tonight, or tomorrow morning. Thank you in advanced for your assistance.
You put an echo before the shebang, so your script is being interpreted by dash, not bash.
dash doesn't include source, because it's not standard.
Correct your shebang and it'll do the trick.
The standard way to source a script is executing it with ..
Instead of source ./myScript.sh, you do . ./myScript.sh. They're the same in bash.

Adding a service startup script for Amazon linux AMI

I am using an Amazon Linux AMI and doing some custom modifications(added an axis2server, etc) on it and saving it as a new AMI. Now what I want to do is when the AMI boots up, start up axis2server(ie.axis2server should automatically start when the instance boots up). For that I used a init script like below and ran the following command:
chkconfig --add axisservice
But when I launch a new instance from my image, the axis2server is not getting started.
I just only need to execute the script /home/ec2-user/axis2-1.6.1/bin/axis2server.sh at startup. Am I missing anything here?
#! /bin/sh
# Basic support for IRIX style chkconfig
###
# chkconfig: 235 98 55
# description: Manages the services you are controlling with the chkconfig command
###
case "$1" in
start)
echo -n "Starting axisservice"
touch ~/temp.txt
cd /home/ec2-user/axis2-1.6.1/bin
./axis2server.sh &
echo "."
;;
stop)
echo -n "Stopping axisservice"
echo "."
;;
*)
echo "Usage: /sbin/service axisservice {start|stop}"
exit 1
esac
exit 0
I went through https://help.ubuntu.com/community/CloudInit as well and it provides a mechanism called User-Data Scripts, where a user can execute a script when launching the script.
$ euca-run-instances --key mykey --user-data-file myscript.sh ami-axxxx
This is a command line option and what I want is something like when I launch the instance through the UI, the script should be started.Therefore, I think the above option can not be used in my case. Please correct me if I am wrong.
Thanks,
H.
I bet the environment is not set(up correctly). This means that I am guessing that your shell script tries to start another program and it's not to be found.
So at first, I'd adjust the start part of your script (current):
echo -n "Starting axisservice"
touch ~/temp.txt
cd /home/ec2-user/axis2-1.6.1/bin
./axis2server.sh &
echo "."
Edited:
echo -n "Starting axisservice"
touch ~/temp.txt
cd /home/ec2-user/axis2-1.6.1/bin
./axis2server.sh
RETVAL=$?
[ $RETVAL -eq 0 ] && echo Success
[ $RETVAL -ne 0 ] && echo Failure
echo "."
So what did I do?
removed & so script waits for your shell script (axis2server.sh) to complete
checked the return status ($?) of your shell script
Further debugging:
Add set -x to your scripts to enable tracing and log both stderr and stdout.
Questions:
Are you are aware that stop (in your service script) doesn't do anything?
touch ~/temp.txt is that supposed to create /root/temp.txt? (I'm guessing root runs this script.)
If none of my suggestions work, can you share axis2server.sh and paste stderr and stdout?

Resources