6

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.

Eric Carvalho
  • 55,453
BDMan32
  • 71
  • 1
  • 3

3 Answers3

7

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?

αғsнιη
  • 36,350
0

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

Frantique
  • 8,673
-1

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

Sapnesh Naik
  • 290
  • 6
  • 19