Need help creating an advanced shutdown script for power-saving?
Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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?
#!/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
#!/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;
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'.
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 05:39 PM..
Reason: Question #1 is invalid
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.
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
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".
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
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
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?
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".
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.