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.

  • 2
    What's the model number of your UPS? I find it difficult to believe that any UPS manufactured in the last fifteen years lacks a USB or serial interface to connect to the powered equipment. – BMDan Jul 12 '11 at 14:20
  • 2
    There are lots of cheap UPS that have no interface, but these are are not usually appropriate for server equipment. – Zoredache Jul 12 '11 at 16:51
  • 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. – Ergec Jul 13 '11 at 6:17

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:

Check host router with address
     if failed icmp type echo with timeout 120 seconds
       then exec "/sbin/shutdown now"
       alert root@localhost

Really the best solution here would be APCUPSD, you ought to get something that has monitoring capabilities (USB,Serial,Ethernet).

  • 1
    I agree that apcupsd would be better, but if you end up using monit maybe you could change the 2nd line to "if failed icmp type echo with timeout 30 seconds for 4 cycles" so if 4 pings are missing the shutdown happens – Jure1873 Jul 12 '11 at 14:26

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 system

After the power is restored I have linksys with openwrt firmware to send wake on lan to my system.

  • The switch/networking gear is almost certainly on the UPS as well. Looking at the link status doesn't seem very helpful here. – Zoredache Feb 27 '12 at 1:56

make a bashsscript and add it to chron :

 $1 = "some.host.ip.address";

    if ! ping -c 1 -w 5 "$1" &>/dev/null ; then 

      shutdown -h now


Bear in mind, if your router fails instead of your server, you are going down...

  • How about just ping -c 1 -w 5 some.host.ip.address || shutdown -h now? Bigger issue here is that even transient problems (e.g. an overloaded router choosing not to reply to a ping) might cause you to shut down. Assuming you're OK with that (and not OK with figuring out how to hook up your UPS), then this will work. – BMDan Jul 12 '11 at 14:19
  • yea, I know I wouldn't use this as a solution, I think it is very dangerous, on the other hand, a server just loosing power is dangerous aswell. – Lucas Kauffman Jul 12 '11 at 14:45
  • BBU on the RAID card and journaling FS? A system suddenly losing power under those conditions is not (unreasonably) dangerous. – BMDan Jul 13 '11 at 12:10

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 the pf: 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:
Defaults:remoteshutdown !requiretty
remoteshutdown ALL=(ALL) NOPASSWD: /sbin/shutdown
  • /etc/inittab on Volta:
  • /usr/local/sbin/powerdown (chmod 700) on Volta:

# Run in the background lest we block on it and thus wait too long to schedule our own shutdown.
# Remember to connect by hand once and accept Nulla's host key as root
/usr/bin/ssh remoteshutdown@Nulla 'sudo /sbin/shutdown -f -h +2 "Power Failure; System Shutting Down"' &

#Shut ourselves down
/sbin/shutdown -f -h +2 "Power Failure; System Shutting Down"

As I said, there's probably another way to do this, but this is the solution I can write without having to download anything. ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.