Ask Your Question
0

Fedora 20, laptop, systemd, udev and power_supply

asked 2014-04-15 03:53:48 -0600

Bad_ptr gravatar image

updated 2014-04-15 06:22:14 -0600

I want to run script when I plug-in/-out power cable into my laptop.
I created these files:

cat /etc/udev/rules.d/91-power.rules

SUBSYSTEM=="power_supply", TAG+="systemd", ENV{SYSTEMD_WANTS}="mypower@$attr{online}.service"

cat /etc/systemd/system/mypower@.service

[Unit]
Description=Power supply handling task

[Service]
ExecStart=/usr/local/bin/mypower.sh %I

cat /usr/local/bin/mypower.sh

#!/bin/bash
case $1 in
  0)
    echo 'off' > /home/data/test
    ;;
  1)
    echo 'on' > /home/data/test
    ;;
  *)
    echo 'wtf' > /home/data/test
    ;;
esac

Then I ran sudo udevadm control --reload, then devadm monitor, then unplug and plug in cable.
Here is the output of devadm monitor:

KERNEL[2672.773310] change   /devices/platform/ACPI0003:00/power_supply/AC0 (power_supply)
UDEV  [2672.803313] change   /devices/platform/ACPI0003:00/power_supply/AC0 (power_supply)
KERNEL[2673.347460] change   /devices/LNXSYSTM:00/device:00/PNP0A08:00/device:02/PNP0C09:00/PNP0C0A:00/power_supply/BAT0 (power_supply)
UDEV  [2673.348692] change   /devices/LNXSYSTM:00/device:00/PNP0A08:00/device:02/PNP0C09:00/PNP0C0A:00/power_supply/BAT0 (power_supply)
KERNEL[2673.352306] change   /devices/LNXSYSTM:00/device:00/PNP0A08:00/device:02/PNP0C09:00/PNP0C0A:00/power_supply/BAT0 (power_supply)
UDEV  [2673.353478] change   /devices/LNXSYSTM:00/device:00/PNP0A08:00/device:02/PNP0C09:00/PNP0C0A:00/power_supply/BAT0 (power_supply)
KERNEL[2679.249270] change   /devices/platform/ACPI0003:00/power_supply/AC0 (power_supply)
UDEV  [2679.280336] change   /devices/platform/ACPI0003:00/power_supply/AC0 (power_supply)
KERNEL[2679.818443] change   /devices/LNXSYSTM:00/device:00/PNP0A08:00/device:02/PNP0C09:00/PNP0C0A:00/power_supply/BAT0 (power_supply)
UDEV  [2679.819827] change   /devices/LNXSYSTM:00/device:00/PNP0A08:00/device:02/PNP0C09:00/PNP0C0A:00/power_supply/BAT0 (power_supply)
KERNEL[2679.823319] change   /devices/LNXSYSTM:00/device:00/PNP0A08:00/device:02/PNP0C09:00/PNP0C0A:00/power_supply/BAT0 (power_supply)
UDEV  [2679.824619] change   /devices/LNXSYSTM:00/device:00/PNP0A08:00/device:02/PNP0C09:00/PNP0C0A:00/power_supply/BAT0 (power_supply)

But it seems like mypower@.service was not started, couse the file /home/data/test doesn't exist.

EDIT: After reboot. Looks loke this rule is working, but service starts only once at boot time and after that doesn't react on events.

edit retag flag offensive close merge delete

Comments

@marcindulak. Thanks. I think I will try 2 variants: first -- with BindsTo and if it fails -- with ExecStart and ExecStop. (Looks like services with @ parameters intended to be run only once.)

Bad_ptr gravatar imageBad_ptr ( 2014-04-15 10:29:14 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2014-04-16 02:46:56 -0600

Bad_ptr gravatar image

Here is the working solution:

cat /etc/udev/rules.d/91-power.rules

SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_NAME}="AC0", ATTR{online}=="0", RUN+="/usr/bin/systemctl start --no-block mypower.service"
SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_NAME}="AC0", ATTR{online}=="1", RUN+="/usr/bin/systemctl stop --no-block mypower.service"

cat /etc/systemd/system/mypower.service

[Unit]
Description=Power supply handling task

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/local/bin/mypower.sh 0
ExecStop=/usr/local/bin/mypower.sh 1
KillMode=none
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2014-04-15 03:53:48 -0600

Seen: 1,174 times

Last updated: Apr 16 '14