Advanced Shutdown Script – Powersaving

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:

# Weekdays
30 02 * * 1-5 /sbin/shutdown -h now
# Weekends
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:

#!/bin/sh
###
# 2/3/09
# adv-shutdown.sh
# 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

ALIVE=0
for IP  in $IP_LIST; do
   ping -q -c1 -w5 $IP
   if [ $? -eq 0 ]; then
      ALIVE=1
      echo “$IP is alive: $(date)” >> /home/user/scripts/adv-shutdown.log
      break
   fi
done

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
fi

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:

###
# 2/3/09
# Advanced Shutdown Script
# http://basskozz.wordpress.com/2009/02/03/advanced-shutdown-script-powersaving/
###
# 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)

About these ads

10 Responses to Advanced Shutdown Script – Powersaving

  1. [...] Shutdown Script – Part 2 (check for running services) I’ve updated my “Advanced Shutdown Script” to now check for running services/processes before shutting down, this way the script will [...]

  2. anon says:

    dood. script doesn’t work. you should be using double quotes around your strings

    IP_LIST=”192.168.1.101 192.168.1.102 192.168.1.103 192.168.1.104″

    not

    IP_LIST=’192.168.1.101 192.168.1.102 192.168.1.103 192.168.1.104′

    you got me on the right path though. just the functionality i need. cheers

  3. DesertDawg says:

    Does any one have a script for windows, that will check to see if and app is running, if it is then it rechecks every 10 minutes, if the application is not running it will reboot the pc.

  4. kostenfreie programmeISoftware Tools Scripte eBooks deutsch kostenlos…

    [...]Advanced Shutdown Script – Powersaving « BassKozz[...]…

  5. What’s up, after reading this remarkable post i am also glad to share my familiarity here with colleagues.

  6. Cathy says:

    Does your site have a contact page? I’m having a tough time locating it but, I’d like
    to send you an email. I’ve got some suggestions for your blog you might be interested in hearing. Either way, great blog and I look forward to seeing it develop over time.

  7. Madeleine says:

    Hello, i believe that i saw you visited my blog so i got here to go
    back the favor?.I’m attempting to find things to improve my web site!I assume its adequate to make use of some of your ideas!!

  8. Does your blog have a contact page? I’m having problems locating it but, I’d like to shoot you
    an e-mail. I’ve got some suggestions for your blog you might be interested in hearing. Either way, great website and I look forward to seeing it grow over time.

  9. I’m trying to nourish a similar site to this myself, there’s clearly a lot of effort
    that goes into it. Really lively community too, which
    is not easy to build.

  10. Thanks in favor of sharing such a nice thinking, paragraph is
    good, thats why i have read it fully

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: