Rebooting Linux from Script
I Have a Backup Script That Gets Executed Daily via a Cron Job. It's Copying from Ftp Servers, and Attached Usb Harddisks, Databases and Etc. at the End of the...
I have a backup script that gets executed daily via a cron job. It's copying from FTP servers, and attached USB harddisks, databases and etc.
At the end of the script, I want to reboot the Linux machine, so I put in the end of the script:
reboot
But typing uptime next day reveals that it didn't reboot. The cron job is executed by the root user, so I shouldn't need to put sudo or su infront of the command, afaik. So instead I tried other variants of the command, like:
shutdown -t 10 -r
shutdown -r now
/sbin/shutdown -t 10 -r
/sbin/shutdown -r now
/sbin/reboot
One attempt at a time of course, not all the above 5 lines after each other. The PC just doesn't reboot.
Does anyone have any ideas about this?
4 Answers
I've found (tested on Raspbian) that the shutdown command works as expected, but the key thing is that the calling script must not exit before the timer elapses. So I added a sleep command:
shutdown --reboot 1 "System rebooting in 1 minute"
sleep 90
Hell of a way to reboot it is to use
reboot -f
But this will reboot your machine in 3 seconds without gracefully stopping any services\apps, so you might consider another way around.
Also shutdown may not recognize the "-t" argument, taking the time argument like this instead:
shutdown -r 10 //reboot in 10 minutes
Anyways, man shutdown may come in handy. It does not appear that shutdown accepts anything less than minute resolution, but your milage may vary.
I found a great answer here and it worked for me on Armbian.
I was even able to reboot the system via script that runs not-elevated.
Try this command
systemctl reboot -iTo shutdown run this
systemctl poweroff -ito hibernate, suspend, hybrid run
systemctl hibernate -i systemctl suspend -i systemctl hybrid-sleep -i
Allow reboot for user using visudo
your_username ALL= /sbin/reboot
In your script change reboot to sudo reboot