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 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 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 For example Code:
loop 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 |
A quick hack:
Code:
#!/bin/sh |
Quote:
Just 2 questions;
-BassKozz |
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 Code:
### See Also: Advanced Shutdown Script - Powersaving Thanks again for all your help, -BassKozz |
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". |
#
# ***Update***: Advanced Shutdown Script - Part 2 (check for running services) Script now checks for any running service(s) prior to shutdown ;) # # Quote:
Thanks for the links, I'll check them out. Quote:
Quote:
How else could you get the script to run every 10minutes? |
Quote:
|
There's more than one way/format to specify a list
Quote:
man 5 crontab |
Ahh, now I see, thanks for that post chrism01,
I ran a test: Code:
0-50/10 * * * * echo "ping $(date)" >> ~/test-cron.log So now my sudo crontab will look like this: Code:
# Run Advanced Shutdown script every 10minutes between 1-5:50am every night I love to hear of easier/simpler ways to do things with bash scripts ;) |
All times are GMT -5. The time now is 03:23 AM. |