8

I'm having an assignment where the lecturer is asking me to create a bash script to shut down a server at 11pm and turn it on at 6am. I'm able to do it by using sudo shutdown -h 23:00 and it works. But the problem is that I have no idea how to turn it on automatically on 6am, I couldn't find any commands that could do that. Any help would be appreciated.

Sylvain Pineau
  • 61,564
  • 18
  • 149
  • 183
JLWK
  • 113
  • 1
  • 2
  • 5
  • Are you sure your assignment asks you to turn off the whole machine (and back on again), or just one server process? Obivously, the physical machine needs to be switched on to do anything. – Jos Mar 24 '14 at 15:32
  • To be honest, I have no idea too, I also thought this would be impossible. The lecturer is being very vague on the assignment brief and its driving me nuts as well. – JLWK Mar 24 '14 at 16:57

1 Answers1

10

First you need to check if you can use the RTC wakealarm to wake your system:

sudo sh -c "echo 0 > /sys/class/rtc/rtc0/wakealarm"
sudo sh -c "echo `date '+%s' -d '+ 3 minutes'` > /sys/class/rtc/rtc0/wakealarm"
cat /sys/class/rtc/rtc0/wakealarm

Now check:

cat /proc/driver/rtc

This should return a list of parameters. Check the alrm_time is 3 minutes into the future and the alrm_date is today.

If it works ok, create a /usr/local/sbin/shutwake script:

#!/bin/bash 
sh -c "echo 0 > /sys/class/rtc/rtc0/wakealarm" 
sh -c "echo `date '+%s' -d '+ 7 hours'` > /sys/class/rtc/rtc0/wakealarm" 
shutdown -h now

Finally edit your user crontab, type crontab -e and add the following line:

0 23 * * * /usr/local/sbin/shutwake
Sylvain Pineau
  • 61,564
  • 18
  • 149
  • 183