2

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:

#!/bin/bash
# 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:

#!/bin/bash
at -f /root/scripts/shutdown_now.sh now + 10 minutes

And the second script (shutdown_now.sh) looks like this:

#!/bin/bash
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.

Peter S.
  • 23
  • 1
  • 5
  • Having tested it on my Debian box just now, it seems to be correct anyway, though wrapping it into `bash -c` isn't necessary, `nohup /sbin/shutdown -h 10 &` seems to work just fine here – Damon Nov 18 '13 at 13:10

3 Answers3

4

Depending on your backup server’s OS, the implementation of shutdown might behave differently. I have tested the following two solutions on Ubuntu 12.04 and they both worked for me:

As the root user I have created a shell script with the following content and called it in a bash shell:

shutdown -h 10 &
exit 0

The exit code of the script in the shell was correct (tested with echo $?). The shutdown was still in progress (tested with shutdown -c).

This bash function call in a second shell script worked equally well:

my_shutdown() {
  shutdown -h 10
}
my_shutdown &
exit 0
Chriki
  • 15,638
  • 3
  • 51
  • 66
  • Thank You for Your assistance! But somehow this does't seem sufficient to make bacula believe the script has completed successfully. – Peter S. Nov 18 '13 at 14:28
  • I could imagine that your backup script process is still the parent process of the shutdown process – even though we have tried to fork it. Bacula then notices that there is still some child process around and blocks until the shutdown happens. On my machine, the shutdown process seems to be a new process; but this might again be OS dependent. You could try a bit harder to “daemonize” your shutdown command like so: `nohup shutdown -h 10 0<&- &>/dev/null &` ([via](http://stackoverflow.com/a/10908325/1797912)) On my machine this doesn’t appear to change anything, but maybe it does for you. – Chriki Nov 18 '13 at 15:41
  • Thank You for that suggestion. I will try it out in the next days. In the meanwhile I go with the at-scheduler, which solves the issue, too. But as I am pretty curious, I will test Your suggestion nevertheless. PS: I use Debian Jessie. – Peter S. Nov 20 '13 at 18:52
3

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.

Brian Showalter
  • 4,321
  • 2
  • 26
  • 29
  • This works like a charm. I wonder however, what is the reason, that You redirect the output of the at command. Is it some kind of "best practices" in shell programming? – Peter S. Nov 20 '13 at 18:49
  • 1
    Putting commands such as this one that generate informational output into automated jobs will sometimes result in emails to root each and every time the job is run. It's just my personal preference to discard such output and prevent those sorts of emails. Of course, this does mean that potential error messages could be lost, so one has to weigh that possibility as well. – Brian Showalter Nov 20 '13 at 20:11
0

If you can become root: either log in as, or sudo -i this works (tested on ubuntu 14.04):

# shutdown -h 20:00 & //halts the machine at 8pm

No shell script needed. I can then log out, and log back in, and the process is still there. Interestingly, if I tried this with sudo in the command line, then when I log out, the process does go away!

BTW, just to note, that I also use this command to do occasional reboots after everyone has gone home.

# shutdown -r 20:00 & //re-boots the machine at 8pm

Aaron
  • 428
  • 5
  • 14
  • Is it necessary to put `nohup` before? like `nohup shutdown -h 20:00` ? What would be an advantage? – KansaiRobot Feb 25 '23 at 05:39
  • 1
    @KansaiRobot If you become root, then no, nohup isn't needed. But if you used sudo, then yes a nohup would be needed. Man nohup. – Aaron Feb 25 '23 at 18:43