My NAS box is a power-hungry beast (Ubuntu 8.10 – p4 3ghz 4GB ram, 3x250gb RAID5, 200gb OS), and while I know I could go out and buy a readymade NAS, what would be the fun in that?, and I could also build a VIA or ATOM based system to do simple NAS tasks and replace the power-hungry p4, that would cost money, and I am trying to pinch-a-penny in these economic times…
So I usually leave the NAS box on 24/7 but I am starting to feel bad about that, and my electricity bill is making me feel it
But I need to leave the NAS on at night because it is my media server for my TV’s (XBMC)…
So I created 2 crontab lines:
30 02 * * 1-5 /sbin/shutdown -h now
30 04 * * 6-7 /sbin/shutdown -h now
This is great, I just turn the NAS on in the morning when I need it… but this isn’t the optimal setting because I usually go to sleep well before 2:30am on weeknights and before 4:30am on weekends, however there are a few nights that I stay up past my bedtime :-P So I wanted to account for that in crontab. But on average I would have to say I usually go to sleep about 2-3hrs before these times.
So I gots-ta-thinkin and I figured that there must be a better way to handle this with a script, and after a quick question to Linux Questions: Need help creating an advanced shutdown script for power-saving?
And I got my answer and here is my new adv-shutdown.sh script:
# Advanced Shutdown Script
# Ref – http://basskozz.wordpress.com/2009/02/03/advanced-shutdown-script-powersaving/
IP_LIST=’192.168.1.101 192.168.1.102 192.168.1.103 192.168.1.104‘
for IP in $IP_LIST; do
ping -q -c1 -w5 $IP
if [ $? -eq 0 ]; then
echo “$IP is alive: $(date)” >> /home/user/scripts/adv-shutdown.log
if [ $ALIVE -eq 0 ]; then
echo “***ShutDown*** $(date)” >> /home/user/scripts/adv-shutdown.log
echo “###########################” >> /home/user/scripts/adv-shutdown.log
/sbin/shutdown -h now
see also: http://pastebin.com/f3777e743
Simply place this script in your /home/user/scripts directory (if ‘scripts’ directory doesn’t exist ‘mkdir scripts’ from your home directory) replace the IP_LIST ip addresses to match your own (leave a space in-between ip addresses), and replace all of the paths from ‘user’ to your local user account name, and remember to grant execute permissions by issuing “sudo chmod +x adv-shutdown.sh“.
This script will keep a log of shutdown tries in /home/user/scripts/adv-shutdown.log
Now just add this to the root crontab by running ‘sudo crontab -e‘ and enter the following:
# Advanced Shutdown Script
# Run Advanced Shutdown script every 10minutes between 1-5:50am Mon,Tues,Wed,Fri,Sat,Sun
0,10,20,30,40,50 01-05 * * 1,2,3,5,6,7 /home/user/scripts/adv-shutdown.sh
# Run Advanced Shutdown script every 10minutes between 2-5:50am Thurs (S3 Backup Night)
0,10,20,30,40,50 02-05 * * 4 /home/user/scripts/adv-shutdown.sh
see also: http://pastebin.com/f585c9b3c
I created two lines here because my NAS box backs up to Amazon S3 on Thursday nights, so I wanted to give it a little more time to work.
UPDATE – new script:Advanced Shutdown Script – Part 2 (check for running services)