I want to shutdown my linux server right after power outage while it's on UPS. UPS runs like for extra 30 mins. I know that some UPSs can shutdown servers automatically but this is not my case now.
So I've in my mind to ping my router (or a PC in the network) and if pinging fails this means power outage (there may be a problem with the network too but never mind) and script will run
"#shutdown now"
How can I do this?
Edit: UPS is feeding two small servers so I can't use serial port on it (at least for both of them at the same time) . Our powerplant in Cyprus destroyed by a blast so until I get UPS for each server I need a temporary solution. That's why I'm asking this.
are you using an apc ups ? If so look into apcupsd, it's great and can do this and many other similar things with ease.
as bmdan says, it's very unlikely your ups cant do this.
Many Linux systems include the watchdog package. This package will watch for certain conditions by running tests, and by default will reboot the system if there is a failure.
With the repair-binary option you can tell it to run something like
shutdown -h
instead of rebooting. Watchdog includes a ping test by default.Monit can do this and would be preferred to cron.
Try a config line such as:
Really the best solution here would be APCUPSD, you ought to get something that has monitoring capabilities (USB,Serial,Ethernet).
On Ubuntu I use
mii-tool
to check that LAN link is OK If the link is not OK for 3 times then I poweroff the systemAfter the power is restored I have linksys with openwrt firmware to send wake on lan to my system.
make a bashsscript and add it to chron :
Bear in mind, if your router fails instead of your server, you are going down...
If you are using NUT (Network UPS Tool) to monitor your server, then your clients can do this. The host to which the UPS is connected should be the last to go down. NUT server runs on UNIX or Linux, but a client is available for Windows.
There is an option to run a script on various events (with delays if desired). In your case you would want to trigger shutdown on an outage of some seconds. If you have wake on LAN you you could trigger a wake event some time after power is restored.
Based on the two-servers-one-UPS problem, this solution now gets a lot simpler (and more robust!). There are probably better ways to do this, but my approach would be to add a user who can run
sudo /sbin/shutdown -h now
on the box not plugged in to the UPS, and then modify thepf:
entry in /etc/inittab on machine #1 to (try to) create a ssh connection to machine #2 and run that command before it completes its own shutdown.Complete implementation for two machines, where Volta (with UPS serial connection) and Nulla (no UPS connection) are the two machines' names:
/etc/sudoers
on Nulla:/etc/inittab
on Volta:/usr/local/sbin/powerdown
(chmod 700) on Volta:As I said, there's probably another way to do this, but this is the solution I can write without having to download anything. ;)