Run the script when connecting / disconnecting the monitor

I read a lot of topics on how to do this, and yet it does not work for me.

What I want to achieve:

When my laptop has two additional displays connected, when I close its cover, I do not want it to sleep. However, if I turn off the displays and the cover is closed, I want to put the laptop into sleep mode (thus, not forgetting it).


Therefore, I created a BASH script that should be executed on VGA / HDMI connection events. The BASH script counts how many displays are connected, and if there is only 1, it will make the laptop sleep when the cover is closed.

I have Ubuntu 14.04 LTS. This is what I have done so far:

SUBSYSTEM=="drm", RUN+="/bin/bash /home/nir/dev/scripts/displays_count_sleep.sh"
  • Put the BASH script I want to run, display_count_sleep.sh , in /home/nir/dev/scripts:
#!/bin/bash

DISPLAYS_NUM=2

`touch test`

display_count=`xrandr -d :0 -q | grep ' connected' | wc -l`

echo "display count="$display_count
echo "display_num="$DISPLAYS_NUM

if [ "$display_count" -ge "$DISPLAYS_NUM" ]; then
    echo "nothing"
    `gsettings set org.gnome.settings-daemon.plugins.power lid-close-ac-action nothing`
    `gsettings set org.gnome.settings-daemon.plugins.power lid-close-battery-action nothing`
else
    echo "sleep"
    `gsettings set org.gnome.settings-daemon.plugins.power lid-close-ac-action suspend`
    `gsettings set org.gnome.settings-daemon.plugins.power lid-close-battery-action suspend`
fi

Both files were created as root. When you run " udevadm test", the output will be the same as in the " udevadm_test_output " file in the link.

I do not know if the event is caught, but on a real monitor (dis) connection, the script does not start. It works fine if I run it manually.

+4
source share

All Articles