3

I'm trying to change some settings with the help of udev when i plug in and out the ac adopter but cant seem to get it to run. I'm running ubuntu 15.04 and this is what I've got:

sudo systemctl status udev
● systemd-udevd.service - udev Kernel Device Manager
   Loaded: loaded (/lib/systemd/system/systemd-udevd.service; static; vendor preset: enabled)
   Active: active (running) since lör 2015-05-02 20:28:42 CEST; 2h 3min ago
     Docs: man:systemd-udevd.service(8)
           man:udev(7)
 Main PID: 293 (systemd-udevd)
   CGroup: /system.slice/systemd-udevd.service
           └─293 /lib/systemd/systemd-udevd
agni% ls -l /etc/udev/rules.d/50*
-rw-r--r-- 1 root root 122 maj  2 20:25 /etc/udev/rules.d/50-kappa.rules

This is what the rule contains i have tried a couple of different things but this is whats in it currently, and from what I understand it should work

cat /etc/udev/rules.d/50-kappa.rules 
ACTION=="change", SUBSYSTEM=="power_supply", RUN+="/usr/local/bin/power-change.sh true"

and the script that should run

ls -l /usr/local/bin/power-change.sh 
-rwxr-xr-x 1 root root 255 maj  2 17:24 /usr/local/bin/power-change.sh 
cat /usr/local/bin/power-change.sh 
set_ac() {
    echo -e "$(date)\nAC CONNECTED" > /home/kempe/test2.txt
}

set_bat() {
    echo -e "$(date)\nAC DISCONNECTED" > /home/kempe/test2.txt
}

case $1 in
    true) set_ac ;;
    false) set_bat ;;
    *) exit 1 ;;
esac

exit 0

And this is what i have done to try to make it run with out any luck

sudo udevadm control --reload    #no effect
sudo udevadm trigger    # no effect
sudo reboot    # no effect :)
udevadm monitor --property
monitor will print the received events for:
UDEV - the event which udev sends out after rule processing
KERNEL - the kernel uevent

KERNEL[816.963544] change   /devices/LNXSYSTM:00/LNXSYBUS:00/ACPI0003:00/power_supply/ADP1 (power_supply)
ACTION=change
DEVPATH=/devices/LNXSYSTM:00/LNXSYBUS:00/ACPI0003:00/power_supply/ADP1
POWER_SUPPLY_NAME=ADP1
POWER_SUPPLY_ONLINE=0
SEQNUM=2847
SUBSYSTEM=power_supply

UDEV  [816.966150] change   /devices/LNXSYSTM:00/LNXSYBUS:00/ACPI0003:00/power_supply/ADP1 (power_supply)
ACTION=change
DEVPATH=/devices/LNXSYSTM:00/LNXSYBUS:00/ACPI0003:00/power_supply/ADP1
POWER_SUPPLY_NAME=ADP1
POWER_SUPPLY_ONLINE=0
SEQNUM=2847
SUBSYSTEM=power_supply
USEC_INITIALIZED=6963257

KERNEL[817.171654] change   /devices/virtual/backlight/mba6x_backlight (backlight)
ACTION=change
DEVPATH=/devices/virtual/backlight/mba6x_backlight
SEQNUM=2848
SOURCE=sysfs
SUBSYSTEM=backlight

UDEV  [817.174385] change   /devices/virtual/backlight/mba6x_backlight (backlight)
ACTION=change
DEVPATH=/devices/virtual/backlight/mba6x_backlight
SEQNUM=2848
SOURCE=sysfs
SUBSYSTEM=backlight
SYSTEMD_WANTS=systemd-backlight@backlight:mba6x_backlight.service
TAGS=:systemd:
USEC_INITIALIZED=8174

If i run the script directly from terminal it works fine so there is no problem there

/usr/local/bin/power-change.sh true
agni% cat /home/kempe/test2.txt 
-e lör  2 maj 2015 23:06:52 CEST
AC CONNECTED

Any suggestions?

4
  • Could you please take some time to check my answer, if it works?
    – user.dz
    Sep 10, 2015 at 14:50
  • I will check it again tomorrow just to be sure, but it did not work at the time you answered
    – Kempe
    Sep 10, 2015 at 18:23
  • I have add a command to test & debug , could you please try it?
    – user.dz
    Sep 19, 2015 at 9:06
  • 1
    I dont have that installation left anymore so i cant test it. I dont know what it was that made it not work, but i tried the real script today on my fedora install and it all worked as it should
    – Kempe
    Sep 20, 2015 at 21:21

1 Answer 1

3

#!/bin/sh ... Where is the hashbang my friend?

  • The hashbang is missing in the script:

    udev does not run these programs on any active terminal, and it does not execute them under the context of a shell. Be sure to ensure your program is marked executable, if it is a shell script ensure it starts with an appropriate shebang (e.g. #!/bin/sh), and do not expect any standard output to appear on your terminal.

    source: Writing udev rules

  • Another way, call it with /bin/sh

    ACTION=="change", SUBSYSTEM=="power_supply", RUN+="bin/sh /usr/local/bin/power-change.sh true"
    

I was wrongly looking for what going on in the rule for around two hours. That a lesson for me too.

BTW, You can debug and test your udev setup.

sudo udevadm test /sys/devices/LNXSYSTM:00/LNXSYBUS:00/ACPI0003:00/power_supply/ADP1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .