LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 02-02-2009, 03:31 PM   #1
BassKozz
Member
 
Registered: Dec 2008
Distribution: Ubuntu 8.10
Posts: 35

Rep: Reputation: 15
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
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 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

Last edited by BassKozz; 02-02-2009 at 03:57 PM.
 
Old 02-02-2009, 04:01 PM   #2
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
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
 
Old 02-02-2009, 04:06 PM   #3
BassKozz
Member
 
Registered: Dec 2008
Distribution: Ubuntu 8.10
Posts: 35

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by crabboy View Post
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
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

Last edited by BassKozz; 02-02-2009 at 04:39 PM. Reason: Question #1 is invalid
 
Old 02-03-2009, 09:18 AM   #4
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
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.

Last edited by crabboy; 02-03-2009 at 09:21 AM.
 
Old 02-03-2009, 02:16 PM   #5
BassKozz
Member
 
Registered: Dec 2008
Distribution: Ubuntu 8.10
Posts: 35

Original Poster
Rep: Reputation: 15
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

Last edited by BassKozz; 02-04-2009 at 11:54 AM.
 
Old 02-04-2009, 11:04 AM   #6
javaunixsolaris
Member
 
Registered: Jan 2007
Location: Colorado
Distribution: Kubuntu
Posts: 47

Rep: Reputation: 15
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".
 
Old 02-04-2009, 11:34 AM   #7
BassKozz
Member
 
Registered: Dec 2008
Distribution: Ubuntu 8.10
Posts: 35

Original Poster
Rep: Reputation: 15
#
#
***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 View Post
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
Thanks for the links, I'll check them out.
Quote:
Originally Posted by javaunixsolaris View Post
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 View Post
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?

Last edited by BassKozz; 02-04-2009 at 11:57 AM.
 
Old 02-05-2009, 04:32 PM   #8
javaunixsolaris
Member
 
Registered: Jan 2007
Location: Colorado
Distribution: Kubuntu
Posts: 47

Rep: Reputation: 15
Quote:
Originally Posted by BassKozz View Post
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.
 
Old 02-05-2009, 07:12 PM   #9
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,289

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
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
 
Old 02-05-2009, 09:18 PM   #10
BassKozz
Member
 
Registered: Dec 2008
Distribution: Ubuntu 8.10
Posts: 35

Original Poster
Rep: Reputation: 15
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
 
  


Reply

Tags
cron, crontab, samba, shutdown


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Want to write shutdown script in case of power failure cadcrazy Programming 10 10-07-2008 07:28 AM
How do you shutdown Advanced Power Management in Linux jacatone Linux - Newbie 2 05-21-2007 08:19 PM
Power-saving power extension cables? (UK) neocookie General 3 03-28-2007 02:53 PM
Advanced Power Management rdanda Slackware 6 04-09-2003 08:59 PM
Batch Script to shutdown the system in the event of power loss scottpioso Linux - Hardware 5 03-12-2003 03:40 PM


All times are GMT -5. The time now is 11:32 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration