What are good program or script solutions to run specific applications depending on whether the laptop is running on battery or charging?

In order to conserve battery, I automatically want to stop certain applications from running when on battery (Dropbox, backup engine, etc...) and restart them when back on charging.

  • Take a look at the script discussed in this blog post: noobslab.com/2013/10/…
    – slm
    Oct 1, 2014 at 11:08
  • @slm I cannot see how the linked information refers to my question of running certain applications depending on the AC stage.
    – orschiro
    Oct 1, 2014 at 20:00

2 Answers 2


If you are using Ubuntu, You can below function

    if upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep -qP "state:\s*discharging"
        return 1
        return 0
  • I am not using Ubuntu but Arch Linux. However, I also have upower installed. Where should I place this function?
    – orschiro
    Oct 1, 2014 at 13:26
  • Hi, You can use monit deamon to check charging status on specific intervals like every 10 seconds, and you can write monit script with help of above function, to close and open application. take look at below link : mmonit.com/monit Oct 2, 2014 at 14:56
  • Thanks for mmonit, looks what I need! Mind helping me with my ~/.monitrc? I struggle with translating the above function into monit's language. This is what I have so far but I don't know how to properly add the grep/status component: check program upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep -qP "state:\s*discharging" if status != 0 then exec "/usr/bin/dropboxd"
    – orschiro
    Oct 3, 2014 at 5:40

The on_ac_power command allows you to detect whether the machine is plugged to the AC wall plug or running on batteries.

if /usr/bin/on_ac_power 
   echo "Plugged to the AC"
   echo "Running on batteries"

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.