simple scheduled sleep and wake up for a Ubuntu server

For most media servers and file sharing servers in a home network, they usually don’t need to run 24/7, and manually suspend these servers overnight can be very annoying. To avoid this annoyance and save little bit of your hydro bill, you can set a scheduled sleep for your Ubuntu server with crontab and a very short bash script.

 

The first thing to do is create a short bash script

Here is a sample

#!/bin/bash
HONGYA=`date -d "now + 8 hour + 0 min + 0 sec" +%s`
rtcwake -lv -m mem -t $HONGYA
exit

the red colored part defines the duration of the sleep state; so change the red colored part as you wish

then change its permission to 700; hence it can be executed by its owner. Also change the ownership of this file to root for better security.

For example, if this bash script is saved at ‘~/schedule_sleep’, then execute the following command

sudo chmod 700 ~/schedule_sleep

With the ‘rtcwake‘ command, you can do more than just suspending the system to memory; thus check the man page of ‘rtcwake’

 

 

Next, crontab needs to be edited; so the system can be suspended at a specific time

to edit crontab, execute the following command

sudo nano /etc/crontab

Here is a sample of crontab

# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user  command
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
00 23   * * *   root    /home/username/schedule_sleep
#

the green colored part defines the time at which the previousely created bash script will be executed, and the blue colored part defines the location of the bash script; so those two parts as you wish

 

As a result, the system will be suspended at 23:00 daily and will wake up 8 hours later

 

If you want it behaves as wakeupalarm, you may simply add a line in crontab that plays music after the system wakes up from the sleep

For example,

01 8    * * *   your_username    amarok -p

 

In case, if you need to wake up the system before the scheduled sleep, you could use ‘wake-on-lan’. Further information can be found at ‘WakeOnLan – Community Ubuntu Documentation

 

One thought on “simple scheduled sleep and wake up for a Ubuntu server

  1. thanks for this tip!

    I used this to schedule sleep between (daily) backups using rsnapshot on an ubuntu 12.04 server backing up another ubuntu server on my local network. I had to -not- use the -l option for rtcwake to make it wake at the expected time, and i used sudo crontab -e to edit the cron config file. in the case of crontab -e, i had to remove the ‘root’ user specification, as this cron config takes no user, and it was instead trying to execute the command ‘root’, which is nonsensical.

    thanks again.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>