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.
Asked
Active
Viewed 1.2k times
9
Sylvain Pineau
- 63,229
JLWK
- 123
1 Answers
11
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
- 63,229