LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Need help creating an advanced shutdown script for power-saving? (http://www.linuxquestions.org/questions/linux-general-1/need-help-creating-an-advanced-shutdown-script-for-power-saving-701727/)

BassKozz 02-02-2009 03:31 PM

Need help creating an advanced shutdown script for power-saving?
 
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 :p
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:
Code:

# 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 the Ubuntu forums about finding out who is using samba JillSwift replied with:
Code:

net status shares parseable
Which seems perfect for what I need...
Now I have little-to-no skills in scripting, but if someone wouldn't mind helping me out in the name of 'going green' it would be greatly appreciated.

The goal is to create a shutdown script that first checks to see if anyone is using any of the samba shares using 'net status shares parseable', if not shutdown, if so wait 5 minutes and check again.
Here is my rough draft:
Code:

loop
    if 'net status shares parseable' = null
        shutdown -h now
        Exit Loop
    else
        Pause 5minutes

Alternatively, if using samba share activity as a guide isn't a sound option, another option would be to check and see if a specific set of computers is on or off using ping (I have all of my computers on my LAN setup with STATIC IP's so I could just ping their ip's to see if they are active).
For example
Code:

loop
    if 'ping 192.168.1.112' AND 'ping 192.168.1.114' = Destination Host Unreachable
        shutdown -h now
        Exit Loop
    else
        Pause 5minutes

This way with the script I can set it to run in crontab at an earlier time (say 1am on weeknights and 2am on weekends) to shutdown my NAS.
Does anything like this exist already?
Is there a better way to handle this, if so I am all ears?
If not, Can someone lend a hand with this or point me in the direction to get some assistance?

Thanks in advance for any/all help,
-BassKozz

crabboy 02-02-2009 04:01 PM

A quick hack:
Code:

#!/bin/sh

IP_LIST='10.136.1.7 10.136.128.8'

ALIVE=0
for IP  in $IP_LIST; do
  ping -q -c1 -w1 $IP
  if [ $? -eq 0 ]; then
      ALIVE=1
      break
  fi
done

if [ $ALIVE -eq 0 ]; then
  echo shutdown
fi


BassKozz 02-02-2009 04:06 PM

Quote:

Originally Posted by crabboy (Post 3429551)
A quick hack:
Code:

#!/bin/sh

IP_LIST='10.136.1.7 10.136.128.8'

ALIVE=0
for IP  in $IP_LIST; do
  ping -q -c1 -w1 $IP
  if [ $? -eq 0 ]; then
      ALIVE=1
      break
  fi
done

if [ $ALIVE -eq 0 ]; then
  echo shutdown
fi


Thanks crabboy :D
Just 2 questions;
  1. How long does 'break' actually break?
    I don't want this thing to keep slamming my boxes with pings. Can I set it to break for 5minutes and then retry?

    EDIT:You can disregard the first question, I didn't realize 'break' just breaks out of a loop and it doesn't 'pause'.

  2. Does this continue to loop? or does it just run once?
    From the looks of it it just runs once (for each IP address in IP_LIST), and then quits, or maybe I am not reading this right?
Thanks again for the help,
-BassKozz

crabboy 02-03-2009 09:18 AM

You are reading it correctly. The loop identifies the first machine in the list that responds to a ping. As soon as it finds one it will break out of the loop and exit. If it completes the loop not finding a machine that responds, it will execute the shutdown command.

The flags for ping are -q (quiet) -c1 (send only one packet) -w1 (wait for 1 second for a response), the w will be exercised if the machines are down. If you find that the machine shutdown when the machines are still alive, you can try incrementing both the -c and the -w to 2.

You can add a statement to cron to execute the script when you want:

0,15,30,45 2-5 * * * /path/to/script > /dev/null

This will run the script every 15 minutes between 2 and 5 in the morning, but it's been a while since I wrote a crontab entry statement, so you better test this one. :)

BassKozz 02-03-2009 02:16 PM

Thanks crabboy,
Here is my script (slightly tweaked):
Code:

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

and my 'sudo crontab -e':
Code:

###
# 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 > /dev/null
# 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 > /dev/null

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.
See Also: Advanced Shutdown Script - Powersaving
Thanks again for all your help,
-BassKozz

javaunixsolaris 02-04-2009 11:04 AM

For someone with "little" shell script skills you did good. Here's more resources to become a bash bad ass:
http://www.tldp.org/LDP/abs/html/index.html
http://www.tldp.org/HOWTO/Bash-Prog-...OWTO.html#toc7

Also here's my crontab knowledge:
#CRON: minute hour day_of_month month day_of_week COMMAND
I don't do comma's like you, I guess both works.
If you define a MAILTO=you@email.com it will email you when it runs(: I usually make a crontab.job file, install it with "crontab crontab.job" and then list all my crons with "crontab -l".

BassKozz 02-04-2009 11:34 AM

#
#
***Update***: Advanced Shutdown Script - Part 2 (check for running services)
Script now checks for any running service(s) prior to shutdown ;)
#
#
Quote:

Originally Posted by javaunixsolaris (Post 3432046)
For someone with "little" shell script skills you did good. Here's more resources to become a bash bad ass:
http://www.tldp.org/LDP/abs/html/index.html
http://www.tldp.org/HOWTO/Bash-Prog-...OWTO.html#toc7

I couldn't have done it without the help of crabboy :D
Thanks for the links, I'll check them out.
Quote:

Originally Posted by javaunixsolaris (Post 3432046)
If you define a MAILTO=you@email.com it will email you when it runs(: I usually make a crontab.job file, install it with "crontab crontab.job" and then list all my crons with "crontab -l".

Thanks for the pointers ;)
Quote:

Originally Posted by javaunixsolaris (Post 3432046)
I don't do comma's like you, I guess both works.

What do you mean?
How else could you get the script to run every 10minutes?

javaunixsolaris 02-05-2009 04:32 PM

Quote:

Originally Posted by BassKozz (Post 3432073)
What do you mean?
How else could you get the script to run every 10minutes?

All I meant was in the cron itself, you can space delimit.

chrism01 02-05-2009 07:12 PM

There's more than one way/format to specify a list
Quote:

Ranges of numbers are allowed. Ranges are two numbers separated with a
hyphen. The specified range is inclusive. For example, 8-11 for an
"hours" entry specifies execution at hours 8, 9, 10 and 11.

Lists are allowed. A list is a set of numbers (or ranges) separated by
commas. Examples: "1,2,5,9", "0-4,8-12".

Step values can be used in conjunction with ranges. Following a range
with "<number>" specifies skips of the number’s value through the
range. For example, "0-23/2" can be used in the hours field to specify
command execution every other hour (the alternative in the V7 standard
is "0,2,4,6,8,10,12,14,16,18,20,22"). Steps are also permitted after
an asterisk, so if you want to say "every two hours", just use "*/2".
from
man 5 crontab

BassKozz 02-05-2009 09:18 PM

Ahh, now I see, thanks for that post chrism01,
I ran a test:
Code:

0-50/10 * * * * echo "ping $(date)" >> ~/test-cron.log
and sure enough it ran every ten minutes ;-)
So now my sudo crontab will look like this:
Code:

# Run Advanced Shutdown script every 10minutes between 1-5:50am every night
0-50/10 01-05 * * 4 /home/user/scripts/adv-shutdown.sh > /dev/null

Thanks for the feedback javaunixsolaris & chrism01
I love to hear of easier/simpler ways to do things with bash scripts ;)


All times are GMT -5. The time now is 12:56 PM.