Peter S. Peter S. - 2 years ago 110
Bash Question

How can I trigger a delayed system shutdown from in a shell script?

On my private network I have a backup server, which runs a bacula backup every night. To save energy I use a cron job to wake the server, but I haven't found out, how to properly shut it down after the backup is done.
By the means of the bacula-director configuration I can call a script during the processing of the last backup job (i.e. the backup of the file catalog). I tried to use this script:

# shutdown server in 10 minutes
# ps, 17.11.2013
bash -c "nohup /sbin/shutdown -h 10" &
exit 0

The script shuts down the server - but apparently it returns just during the shutdown,
and as a consequence that last backup job hangs just until the shutdown. How can I make the script to file the shutdown and return immediately?

Update: After an extensive search I came up with a (albeit pretty ugly) solution:
The script run by bacula looks like this:

at -f /root/scripts/ now + 10 minutes

And the second script ( looks like this:

shutdown -h now

Actually I found no obvious method to add the required parameters of shutdown in the syntax of the 'at' command. Maybe someone can give me some advice here.

Answer Source

No need to create a second BASH script to run the shutdown command. Just replace the following line in your backup script:

bash -c "nohup /sbin/shutdown -h 10" &

with this:

echo "/sbin/poweroff" | /usr/bin/at now + 10 min >/dev/null 2>&1

Feel free to adjust the time interval to suit your preference.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download