I have a conky setup that displays my battery level. The problem is, whenever I plug/unplug my laptop from the charger I need to run the conky-startup script to get it to display properly. I find this to be a pain (Even though I have a launcher set up that runs the script when clicked on) and would like to know what I need to do to make the script run whenever I plug/unplug my laptop from the charger.
3 Answers
Here's a pretty simple script that can be added as Startup Application and will run continuously. Insert the command you want to run upon detecting change in power method, as specified in the comments.
#!/bin/bash
# Author: Serg Kolo
# Date: June 17,2015
# Description: this script detects changes in
# the powering method, and does something user
# specifies in appropriate field
on_ac_power
PREVIOUS=$(echo $?)
while [ 1 ]; do
# check if we're on ac power or not
on_ac_power
CURRENT=$(echo $?)
# check if previous values are current
# are different. If they are
# raise the flag.
if [ $CURRENT -ne $PREVIOUS ]; then
echo things changed
# Insert commands you wanna run here
# in the space below this comment
echo running custom stuff
# when done: make current value previous
# for future comparison
PREVIOUS=$(echo $CURRENT )
else
# if previous values and current are same
# it means we didn't switch from battery to
# ac or vice versa; do nothing
continue
fi
sleep 1
done

- 105,154
- 20
- 279
- 497
When you plug/unplug a power cable any scripts in /etc/pm/power.d are run with a "true" argument when you plug in and a "false" argument when you unplug.
Adding some script to the end of that file should make it run.
If you have a lot of script to run, you should try adding a line that looks like this:
if [ -f /path/to/the/script ]; then
. /path/to/the/script
fi
You will probably need to ensure that the script in the .../power.d/ directory is executable by:
cd /etc/pm/power.d/
chmod +x ./name_of_file
For more information check out this answer: How can I run a script when the power supply is plugged-in or -out?

- 391
- 3
- 15
Here is a generic approach:
#!/bin/bash
status="$(grep -Po '^charging\s+state:\s+\K.*$' /proc/acpi/battery/BAT0/state)"
if [[ $status = 'charging' ]]; then
##Charging, Do something
elif [[ $status = 'discharging' ]]; then
##Discharging, Do something
elif [[ $status = 'charged' ]]; then
##Charged, Do something
else
##Battery not found, Do something
fi
/proc/acpi/battery/BAT0/state
contains the status of the battery, replaceBAT0
if the battery name is different in your caseThe line starting with
charging state:
of the file contains the status whether the battery is charging, discharging or chargedWe save the status string from that line in the variable
status
Depending on the value of
status
, we can do what we want.

- 91,753
on_ac_power
. It does not return anything, but by checking its exit status withecho $?
you know if it runs on ac or battery. Right now i am too sleepy to concoct a script, but i'll try to figure something out with that later, if you dont find anything better – Sergiy Kolodyazhnyy Jun 17 '15 at 00:04