This question already has an answer here:

My project requires that the power source for the laptop be switched on and off.

Is there a way to make Ubuntu shutdown properly when the external power is lost? It will run the shutdown on its internal battery.

marked as duplicate by Sergiy Kolodyazhnyy, David Foerster, Anwar, Eric Carvalho, waltinator Dec 4 '16 at 21:26

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • If updating isn't working, you need a fresh install. You really need it. It has so many security fixes, not to mention the fixes for Shellshock. Seeing that you are using your computer for a project, use Ubuntu 14.04 so you won't have to update until 2019. – John Scott Oct 10 '14 at 17:17

Try to check on_ac_power command. From man on_ac_power:

NAME
       on_ac_power - test whether computer is running on AC power

SYNOPSIS
       on_ac_power

DESCRIPTION
       on_ac_power  checks  whether  the  system  is  running on AC power (i.e., mains power) as opposed to battery
       power.

OPTIONS
       None.

EXIT STATUS
       0 (true)  System is on mains power
       1 (false) System is not on mains power
       255 (false)    Power status could not be determined

if on_ac_power; then 
    echo "You are on AC-Power" # System is on mains power
 else
    echo "Power Lost"          # System is not on mains power
fi

You need to check the AC-Power status in every X interval of time. It's easiest way that you run it in the background, inside a while loop:

while true
 do
    if on_ac_power; then 
        echo "You are on AC-Power" # System is on main power
     else
        echo "Power Lost"          # System is not on main power
    fi
    sleep [Seconds]
 done

Save your script(ChechMainPWR.sh) and if you want to run the script on startup, so add a line in /etc/rc.local to call your script (ChechMainPWR.sh) + "&" to make it exit. Like this

sh /home/USERNAME/ChechMainPWR.sh $

Reboot and see the changes.

BONUS

If you want to see the alert when it's lost or not connected on your desktop notification, you can use notify-send program.

notify-send "Main Power Lot!" "System is now shutting down"

See man notify-send for more info.

NAME
       notify-send - a program to send desktop notifications

SYNOPSIS
       notify-send [OPTIONS] <summary> [body]

DESCRIPTION
       With  notify-send you can sends desktop notifications to the user via a notification daemon from the command
       line.  These notifications can be used to inform the user about an event or display some form of information
       without getting in the user's way.

Then the final script would be like this:

Final script

while true
 do
    if ! on_ac_power; then    # System is not on main power
        notify-send "Main Power Lot!" "System is going down after 10 seconds"
        sleep 10
        echo YOUR_PASSWORD | sudo -kS  shutdown -h now
        #####^^^^^^^^^^^^^ VERY VERY VERY insecure!   ##########
    fi
    sleep 300 # check main power status every 5 minutes
 done

Warning: The echo YOUR_PASSWORD | sudo -kS shutdown -h now works BUT it is extremely insecure, as your password is written in your history and may also be visible to other users through a process listing.

Then alternative solution is; You can configure your system that sudo someCommand does not require a password(in my case for sudo shutdown). To do that, run sudo visudo and add the following line at the END in the file that opens:

Your_USERNAME  ALL=NOPASSWD: /sbin/shutdown

Then exit the editor and save it (CTRL+x).

Now you can use shutdown command from the command line or in your script without password.

So the script would be like this:

while true
 do
    if ! on_ac_power; then  #your system is not on AC-Power
        notify-send "Main Power Lost!" "System is going down after 10 seconds"
        sleep 10
        sudo shutdown -h now  # System is not on mains power
    fi
    sleep 300 # check main power status every 5 minutes
 done

External links:

How do I run specific sudo commands without a password?
How do I restart /shutdown from a terminal?

  • That is perfect, thank you so so much – BDMan32 Oct 10 '14 at 21:30

At Power Management Preferences you can set what to happen when the battery power is critically low.

Set it there to shut down:

enter image description here

  • 1
    I think he is asking if it can do it, when the external power supply is turned off, not when battery is low. – Dan Johansen Oct 10 '14 at 12:26
  • @DanJohansen: What's the difference? On power loss the battery will drain until critical level reached, than it shuts down. – Frantique Oct 10 '14 at 14:27
  • i considered that but I need the battery not to be fully dead in case I need to remove it and run it on better for updates etc – BDMan32 Oct 10 '14 at 21:13

Go to system settings by clicking the settings-icon at the top right corner of your desktop and selecting "system settings..."

In system settings pannel Cick on "power" and there you can see an option for "when power is critically low it black by default. select it and choose 'power off' in the drop down menu.

enter image description here

  • 1
    This is a repeated answer. What's the difference between your answer and the answer above? – Parto Oct 10 '14 at 13:23
  • 1
    @Parto Having been posted pretty soon after the other one, this may have been independently written. This also presents the idea a little differently. With that said, I don't think either this or that answer what the OP asked. Since the solution they propose is an alternative that's sometimes applicable to the OP's situation, and this may even be an XY-problem where this is really what the OP (or others searching) wants, they probably shouldn't be deleted via review (or by mods) though. – Eliah Kagan Oct 10 '14 at 17:00

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