4

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:

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.

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
nbtk
  • 3,039
  • 4
  • 21
  • 19
  • Related: http://askubuntu.com/questions/145342/disabling-monitor-reconfiguration-when-closing-lid, https://help.ubuntu.com/community/LaptopLidAndDockScripts – ivan_pozdeev May 15 '16 at 09:00
  • @ivan_pozdeev The event I want to catch is the HDMI / VGA connections, not he close lid – nbtk May 16 '16 at 07:49
  • Sorry. The formulations are somewhat messy so I got confused which event you cannot catch. The trigger was incorrect usage of "DRM" (the tag relates to its other meaning). – ivan_pozdeev May 16 '16 at 12:35
  • Backticks aren't needed except when you're catching the output into a variable. – ivan_pozdeev May 16 '16 at 13:11
  • @ivan_pozdeev Didn't understand what did you mean in the last comment? – nbtk May 16 '16 at 22:45
  • Why don't you know if the event is catched... Why not create a file on the event so you can check if event has been catched? – Luis Colorado May 17 '16 at 05:50
  • @LuisColorado I tried to use 'touch' in the RUN string. Didn't work =\ – nbtk May 17 '16 at 10:38
  • @nbtk, so you are answering yourself. – Luis Colorado May 17 '16 at 10:48
  • @LuisColorado Not quite. I still don't know how to fix the event catching... I run 'udevadm monitor', and the parameters I listen to are correct. (subsystem=drm). I also tried with action=change. Just doesn't work – nbtk May 17 '16 at 10:55

0 Answers0