Linux Simple bash script to shutdown a system during power outages

viridian

Disciple
I have the misfortune of owning one of those el cheapo UPSes (500VA) which don't allow for a USB/serial connection. I run a small home server that stores my data and allows me to tunnel out of restrictive networks (thanks SSH). My problem is that when the power goes out, the UPS lasts about 40 minutes before crying to death. Since this box is usually doing something, i wanted it to shut down cleanly (I do trust EXT4 with my data, but just to be on the safe side) before the UPS craps out. I'm sure this has been done before and there are probably better ways to do it, but I was too lazy to google it. This script is reliant on my weird setup which brings the network down when the power goes down. It won't work if you have a running network when the power goes out :p.

The logic is pretty simple. The script is supposed to be run as a cron job for root . It polls the network link status every 5 mins and if the link is down, it waits another 10 minutes before shutting down the system. My system is set to start without hitting the power button; I think this can done in most computers. In the future, I will add email notification (sent on next boot).

After downloading the script, place it somewhere in your path (eg. /usr/local/bin). Ensure that you've chmod'ed the script correctly (e.g. chmod 700). Next add an entry to root's crontab

# sudo crontab -e

You should get the screen to edit crontab. Add an entry similar to the following

*/5 * * * * /path/to/the/script/name_of_the_script.sh
(If you have a crappier UPS you can probably change the time to poll to 1-2 minutes, it isn't resource intensive so I don't see any issues)

Additional note: The script waits for 10 minutes after finding the link down and then checks again before shutting down. You can tweak this to your liking.

You can get it from here: Bash script to shutdown in case of no network connectivity - ritesh's posterous
Or here: RAW OUTPUT ihw1WdLS

Appreciate your feedback & criticisms.
 
Hi,

What distro does this script work on?

My distro does not seem to have ethtool present, so it does not work. maybe I need to install it.
 
Nice script..you can also use plain old ifconfig to find out the status of the interfaces..

unfortunately i cant use it because my router is connected to the ups as well :( i'll have to keep looking for some alternate means..
 
shantanugoel said:
Nice script..you can also use plain old ifconfig to find out the status of the interfaces..
unfortunately i cant use it because my router is connected to the ups as well :( i'll have to keep looking for some alternate means..
Yes you're right, it can be done with ifconfig too. I picked ethtool for some reason but I can't remember why :( . So far it's been working great. I will use the output from the log and plot a nice graph that is partially representative of the hardships and forced downtime faced by Bangaloreans :).

Perhaps a better idea would be to listen to the annoying UPS beeps and then shutdown the computer. The frequency should be fairly easy to distinguish from the background noise, since it's set to annoying. With the flip side that anyone with a beeping device can shutdown your system :D
 
viridian said:
Yes you're right, it can be done with ifconfig too. I picked ethtool for some reason but I can't remember why :( . So far it's been working great. I will use the output from the log and plot a nice graph that is partially representative of the hardships and forced downtime faced by Bangaloreans :).

Perhaps a better idea would be to listen to the annoying UPS beeps and then shutdown the computer. The frequency should be fairly easy to distinguish from the background noise, since it's set to annoying. With the flip side that anyone with a beeping device can shutdown your system :D

Now, thats an innovative idea :) And I can use it as well.. I'll see if I can use it somehow..
 
@OP - Cheers! Great idea!
viridian said:
Perhaps a better idea would be to listen to the annoying UPS beeps and then shutdown the computer. The frequency should be fairly easy to distinguish from the background noise, since it's set to annoying. With the flip side that anyone with a beeping device can shutdown your system :D
Not all UPSs make those annoying beeps, e.g. my APC doesn't, but yes, we're going somewhere with that idea.
 
^ You probably own a better APC UPS which allows you to configure the beep nonsense via PowerChute or something.

I'd considered that sound thing a long time ago but dealing with any form of sound manipulation on linux is quite painful and couldn't possibly be accomplished with bash only.

Theoretically it should be as easy as reading from a special file like /dev/dsp/mic1, but in reality you need to wade through a lot of crap to get there. You'd probably need to read up on PulseAudio (or whatever shit they're using on your distro) APIs and implement the solution in C/C++/Python (I'm assuming all three of them have libraries that facilitate this) as a daemon.

Perhaps it would be slightly easier to pull of on a distro that uses JACK instead of PulseAudio (e.g. ubuntustudio), since it is made for sound acquisition and processing. Also need a mic constantly hooked up, which is a slight annoyance.

^^ If you do find something do share, I will be doing some research on this resurrected idea too :)
 
shantanugoel said:
btw what abt LED color? we could do something with that as well.. e.g. my UPS has a green LED when power is on and red when power is gone..

You'd need a webcamera always attached to your computer focused on one tight spot. IMHO, quite annoying to have up all the time and quite resource intensive. You'd need to have some sort of image recognition running as a service (openCV might work).
 
How about not connecting the monitor to the UPS?

Then if monitor status is "disconnected" you switch off the system.

Also, take care to not switch off in "sleep" state.
 
hrohit85 said:
How about not connecting the monitor to the UPS?

Then if monitor status is "disconnected" you switch off the system.

Also, take care to not switch off in "sleep" state.

How would you detect that monitor/display is "off"?
 
I've router also powered via UPS.that means this script will not work since it polls every 10 minutes on network link UP or not.will try.thanks

--
 
Back
Top