how can I run my script automatically on ac switching

So, I wan't to run it when start work on AC and this or better other script when start work from battery

I tried

  • /etc/acpi/power.sh
  • adding symlink to /etc/laptop-mode/battery-start

It seems to not working


Solution 1:

You should be able to do what you want with a script in /etc/pm/power.d. Quoting from the HOWTO.hooks file in pm-utils:

How to write a pm-utils hook:

PARAMETERS

A pm-utils hook is simply an executable file that accepts at least one parameter.

For hooks in power.d, the potential values of that parameter are:

  • true -- the hook MUST perform whatever action is appropriate when the system transitions TO battery power.
  • false -- The hook MUST perform whatever action is appropriate when the system transitions FROM battery power.

You might also want to read some of the existing power hooks in /usr/lib/pm-utils/power.d for ideas about how to structure your script.

Note that if you use the pm-utils interface, you'll be told whether you're on AC power or not through the first argument: if $1 is true, then you're switching to battery power, and if it is false then you're switching back to mains power.

Solution 2:

I achieve the similar goal by using acpi.

use /usr/bin/acpi_listen to catch the acpi event you want, on my lapttop, the event that fire when switch to battery is "ac_adapter ACPI0003:00 00000080 00000000". so I create a new event : /etc/acpi/events/switch-to-battery, below is the content:

event=ac_adapter ACPI0003:00 00000080 00000000
action=/etc/acpi/switch-to-battery.sh

Just do whatever you want in ac_adapter.sh. More examples can be found if acpi-support is installed.

Last, execute this command to make the new event to take effect.

sudo service acpid restart