Timing a Linux Server to Suspend and Restart

I run a Ubuntu Linux sever at home for various tasks such as serving out media, backing up files and acting as a personal cloud. I don't need it on all the time, but I don't want to be bothered turning in on and off. A simple fix for this is to make use of the commands rtcwake and crontab. The steps for doing this are as follows:

  1. cron is a service that can be used to schedule events. To run it, in a terminal type sudo crontab -e. It is important to run this using sudo as the rtcwake command needs to be run with root privileges. You will have a crontab file for each user on your machine, but only the one for root will work.
  2. Enter the following line at the bottom of the file:
00 23 *** /usr/sbin/rtcwake -m mem -s 64800

There are a few things to explain here:

  • The first two digits represent the minutes past the hour, and the second two are the hour that my command will run.
  • The three stars are in place of day, week and month, so my command will run every day at 23:00.
  • It is import to include the full path to the rtcwake command. Check with the command which rtcwake that yours is in the same place.
  • The -m mem part tells the rtcwake command to suspend everything and save it to RAM. It is also possible to save to disk, but I didn't have much success with this.
  • the -s 64800 tells the rtcwake command to restart the machine after 64800 seconds or 18 hours which is 17:00.

So now my server will shut itself down at 11pm at night and restart at 5pm every afternoon.

When I explained this to my wife she instantly asked what if you're doing something with the server at 11pm will you loose it all. So there is an interesting question: how could I write a script to check if something important was happening and suspend the shutdown?

Comments