9

For some reason, my laptop's screen brightness does not update when I plug-in or plug-out the power supply. I've looked at quite a few solutions for that problem, but none of them seemed to work well. So, instead of despairing and giving up, I decided to try to turn this into a learning experience and see if I can't write a script that does it for me.

I tried figuring it out on my own, with some help from the internet, of course, but I'm pretty new to bash scripting and Ubuntu in general, so I didn't get far.

What I was able to figure out was that I can find the state of the battery with

$ upower -i /org/freedesktop/UPower/devices/battery_BAT1 | grep -E "state"
state:     charging/discharging

depending on whether or not the AC adapter is plugged in. I don't know if that's even useful, but it's kinda cool, and I didn't know how to use grep before, so what the hell, learning is fun.

Anyways, is there a somewhat clean way to do this? By "clean way" I just mean a way that one with limited bash script experience could comprehend.

One similar question I found that had a good answer is this one, which says the following:

When you plug in/out the AC adapter, the scripts in /etc/pm/power.d get called with >an argument: "true" (if you run on battery), or "false" (if you run with the power >adapter).

The problem is that I don't know how to access that argument from within a script. So, I guess it all boils down to a pretty simple problem, but I thought I'd post the question anyways, in case people had better solutions.

Jasper
  • 193
  • 1
  • 4

1 Answers1

7

You can use on_ac_power to run a script when the power supply is turned on or off.

Try the following in a terminal.

$ on_ac_power
$ echo $?
0              ## Laptop on ac power

$ on_ac_power $ echo $? 1 ## Laptop on battery (not on ac power)

Based on this you can make your script as,

#!/bin/bash
while true
do
    if on_ac_power; then 
        do_something               ## Laptop on power
    else
        do_something_else          ## Laptop on battery
    fi
    sleep 10                       ## wait 10 sec before repeating
done

EDIT:

cron job would be a better idea for running the script at a regular interval instead of using an infinite loop.

Save your script as myscript.sh and put the following content in it,

#!/bin/bash
if on_ac_power; then 
    do_something
else
    do_something_else
fi

Make the script executable from a terminal, chmod +x /path/to/myscript.sh. Open your personal crontab as EDITOR=gedit crontab -e and append the following line in it to run your script every minute.

* * * * * /path/to/myscript.sh 
αғsнιη
  • 35,660
sourav c.
  • 44,715
  • Is it bad practice, to have scripts running infinitely, or is that something that happens regularly and is built in to the system? I guess I was looking for some sort of trigger but if this works, then it's much less complicated than I had assumed. – Jasper Feb 27 '15 at 17:39
  • @TheQZ: I think 'souravc' has given the basic idea..you can write your own based on this :) – heemayl Feb 27 '15 at 17:44
  • @TheQZ Though it runs forever but becomes active only once per 10 sec (you can increase the check time) Look at the question how to repeat-a-command-every-x-interval-of-time. Otherwise you need to write your own daemon program which is much complicated for the beginners. – sourav c. Feb 27 '15 at 17:51
  • Great! Thanks! Looks like that's what I needed. – Jasper Feb 27 '15 at 17:59
  • 3
    A better way is to use udev rule as in https://askubuntu.com/questions/613741/ubuntu-15-04-pm-utils-do-not-look-into-etc-pm-power-d-anymore-what-instead. – Anthony Wong May 10 '16 at 09:44