I am trying to have udev run certains scripts when my laptop is connected to charger or disconnected from it.

To do that I created a file 99-local.rules in /etc/udev/rules.d and placed the following in it:

ACTION=="change", SUBSYSTEM=="power_supply", ATTR{online}=="1", RUN+="/usr/sbin/onchg.sh"
ACTION=="change", SUBSYSTEM=="power_supply", ATTR{online}=="0", RUN+="/usr/sbin/onbatt.sh"

However, with the file in place, the system seems to hang for some time during the "Raise network interfaces" target, which times out after 2min out of the 5min it displays.

If I remove the file, then it's working just fine.

How is udev even related to the network interfaces startup, and how can I make it boot as fast but keeping these rules?

2 Answers 2


One relation is this udevadm settle in the output of systemctl cat networking:

ExecStartPre=-/bin/sh -c '[ "$CONFIGURE_INTERFACES" != "no" ] && [ -n "$(ifquery --read-environment --list --exclude=lo)" ] && udevadm settle'

Consider what man udev has to say about the RUN key:

This can only be used for very short-running foreground tasks. Running an event process for a long period of time may block all further events for this or a dependent device.

Starting daemons or other long-running processes is not appropriate for udev; the forked processes, detached or not, will be unconditionally killed after the event handling has finished.

  • Oh, I see. Is there any way I can make udev omit my rules during boot? They are used to toggle GPU on/off depending whether my laptop is docked, and there is already a similar systemd unit that does the same during boot. Also it doesn't seem they get launched at all, though the hang might be explained by the fact that after switching the GPU it restarts LightDM in the script.
    – akasaka
    Sep 29, 2018 at 13:54
  • @VladislavKorotnev I'd add trace logging (with timestamps) to the scripts to find out where they get stuck and fix that. Oct 1, 2018 at 7:21

The answer provided by Ferenc gave me a clue that the issue might be related to my own scripts.

In fact, the scripts are supposed to switch the GPU and restart the X server (LightDM) whenever I dock/undock my laptop.

So, it seems udev executed the scripts during boot, so they were trying to restart X and obviously failed.

I added the following code to the top of my onbat.sh/onchg.sh and everything now works perfectly:

# Check if LightDM has already arrived
systemctl status lightdm | grep "running"
if (( $not_running > 0 ))
    # LightDM not arrived, exiting for now...
    echo "LightDM not arrived, won't switch GPU."
  • Glad you solved it! You may want to simplify the code to if ! systemctl is-active --quiet lightdm; then exit; fi or similar. Oct 1, 2018 at 17:03

You must log in to answer this question.

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