I read many threads about how to do this and yet it doesn't work for me.
What I want to achieve is:
When my laptop has 2 additional displays connected when I close its lid, I don't want it to go to sleep. However, if I disconnect the displays and the lid is closed, I want to put the laptop to sleep (this way, not forgetting it on).
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's only 1, it will put the laptop to sleep when the lid is closed.
I have Ubuntu 14.04 LTS. This is what I've done so far:
Created 2 files:
displays_count_sleep.sh
andon_hdmi_connected.rules
https://gist.github.com/nbtk123/9ffbf7541e47b9c0015f5c3e9f44b7c9Put this on_hdmi_connected.rules into
/etc/udev/rules.d
to catch the event:
SUBSYSTEM=="drm", RUN+="/bin/bash /home/nir/dev/scripts/displays_count_sleep.sh"
- Put the bash script I want to run, displays_count_sleep.sh, into
/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 have been created as root. When running "udevadm test
", the output is as in the file 'udevadm_test_output' on the link.
I don't know if the event is catched, but on the real monitor (dis)connection, the script doesn't run. It works okay if I run it manually.