Alert via SMS on power failure and hibernate the system

Alert via SMS on power failure and hibernate the system

When using a system to host your important (or critical) operations, being able to get informed of issues while you’re away is a very handy function. Email is good for notifications, but it isn’t always fast enough, and perhaps you don’t want to waste all that battery power to check for emails on-the-go for just that one alert you’ll maybe, perhaps, but definitely receive when you least expect it.

SMS on the other hand, is an almost instant alert method and although length-limited, should do the job just fine for those very important, must have alerts.

For these mission critical instant alerts you will need (in lack of fancier solutions) a working, configured and test 3G USB stick with a functioning SIM in it (nobody said this will not cost you…) connected to the machine, NUT and gammu.


Configuring gammu is covered by a separate tutorial.



We assume NUT is already installed, configured and running correctly.

NUT should also be configured to hibernate the system on power failure / low battery, not shut it down. To do this, edit /etc/ups/upsmon.conf, search for
SHUTDOWNCMD "/sbin/shutdown -h +0"
comment out that line and add /or replace it with
SHUTDOWNCMD "/usr/sbin/hibernate"

That file, /usr/sbin/hibernate does not exist by default. You need create it with the following content, then chmod it 755:

echo disk >/sys/power/state

This procedure is also described in this post.



Edit /etc/ups/upssched.conf and define your timers/events:

CMDSCRIPT /usr/bin/upssched-cmd

PIPEFN /var/run/nut/upssched/upssched.pipe
LOCKFN /var/run/nut/upssched/upssched.lock



AT ONLINE * EXECUTE ups-back-on-power

upssms is set to execute 30 second after power fails and upsgone at 150 seconds (two and a half minutes) after failure.

Make sure /var/run/nut/upssched exists and is owned by the nut user.

Next, edit the command script /usr/bin/upssched-cmd and add the following:

case $1 in
                logger -t upssched-cmd "The UPS has been gone for 30 secs. Warn by sms..."
                /bin/echo "Power failed. System will hibernate soon..." | /usr/bin/gammu --sendsms TEXT +1234567890
                logger -t upssched-cmd "The UPS has been gone for 2 and a half minutes. Hibernating..."
                #/usr/sbin/hibernate      # direct call to hibernate is not allowed by non-root
                /usr/sbin/upsmon -c fsd   # call a shutdown in NUT instead
                logger -t upssched-cmd "The UPS power has been restored."
                /bin/echo "Power restored." | /usr/bin/gammu --sendsms TEXT +1234567890
                logger -t upssched-cmd "Unrecognized command: $1"

This script uses gammu to connect to the GSM device and send the SMS.

The upssms event (which triggers 30 seconds after power failure) will send an SMS alert informing of the situation. The program then sleeps until the next event, upsgone (two minutes and a half after failure) is triggered. It is now that the hibernation script is called and the system will go to sleep.

However, if power is restored before upsgone, the ups-back-on-power event is triggered which send another SMS informing of power restore and cancels all other timers/actions.

You should make sure the configured timers (here 30 and 150 seconds) are appropriate for the backup power duration. If battery reaches critical before the timers are triggered the system will enter hibernation before alerts are sent.



All that’s left to do now is test out things (or simply wait for the next power failure to see how things work… NUT should still hibernate the system on low battery if things fail).  Also make sure hibernation works on your system before changing NUT’s configuration.

All of the trigger events are logged in the system log. You can monitor /var/log/messages for output from the upssched program.


      1. Okey…

        There were a typo (too) in my CURL command in fact.
        So I receive a notification when the power is back but none when there’s a failure.

        Are you sure upssms is correct?
        something like
        AT ONBATT * EXECUTE upssms


  1. Hello!
    I’m trying to adapt your command script in order to call a webpage using CURL
    (curl “….”)
    but I cant make it since I got syntax errors.
    Can you help?

  2. Hello, I was trying make it work many times now, but i’m still not able to force nut send SMS. Gammu is working well when I invoke it manually, UPS monitor is also working, I got always screen notify when the power is down. Could you please help me with it? I’m stuck 🙁 I have found no errors. I also tried to configure just simple notifycmd script but this also not work. I would like to enable SMS notification about power failure from my raspberry pi home network server.


Leave a Reply