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 .
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.
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.