6

I'm currently using a Laptop running Ubuntu 12.04. When I'm at work I plug in a monitor, mouse and keyboard. Then I close my laptops screen and use it as a normal pc. Everytime I plug it in and plug it out, I have to change my system settings, because normally my laptop would suspend to RAM, when I close its screen. Also I have to change where my graphics card should display my desktop.

Is there any way to execute some script when an external monitor or usb-device is plugged in or out? If yes, is there any way to change my system settings by a shell script? If not, is there any other way to do it?

3
  • I managed to execute a shell script by adding a filne named 90-monitor-hotplug.rules in my /etc/udev/rules.d containing the single line SUBSYSTEM=="drm", ACTION=="change", RUN+="/path/to/executable.sh"
    – Wauzl
    Sep 25, 2012 at 7:37
  • I also managed to change the power settings from a shell. This works using gsettings. For example to make ubuntu suspend when the lid is closed while in battery mode type gsettings set org.gnome.settings-daemon.plugins.power lid-close-ac-action 'suspend'
    – Wauzl
    Sep 25, 2012 at 7:55
  • I also found out how to change the behaviour of my monitors. This cannot be done by gsettings as it is directly done with xrand. For a list of available outputs type xrandr-tool outputs and for turning off for example the VGA output type xrandr --output VGA1 --off and for turning it on again xrandr --output VGA1 --auto. I guess this can be done better, but it will suffice for me. I'll try and plug everything I know now together and post an answer myself.
    – Wauzl
    Sep 25, 2012 at 8:02

1 Answer 1

5

At first – in order to use this method – you need to see if your graphics card sends an udev-event, when a VGA monitor is plugged in. To do this, open a terminal, type udevadm monitor --property and plug a monitor in. You should see something like this:

KERNEL[7671.540341] change   /devices/pci0000:00/0000:00:02.0/drm/card0 (drm)
ACTION=change
DEVNAME=dri/card0
DEVPATH=/devices/pci0000:00/0000:00:02.0/drm/card0
DEVTYPE=drm_minor
HOTPLUG=1
MAJOR=226
MINOR=0
SEQNUM=2296
SUBSYSTEM=drm
UDEV_LOG=3

UDEV  [7672.099723] change   /devices/pci0000:00/0000:00:02.0/drm/card0 (drm)
ACTION=change
DEVNAME=/dev/dri/card0
DEVPATH=/devices/pci0000:00/0000:00:02.0/drm/card0
DEVTYPE=drm_minor
HOTPLUG=1
MAJOR=226
MINOR=0
SEQNUM=2296
SUBSYSTEM=drm
TAGS=:udev-acl:
UDEV_LOG=3
USEC_INITIALIZED=20836591

If you don't get an event, then it is not possible to use this method.

Next, a rule for udev is created. This can be done by creating a file /etc/udev/rules.d/90-monitor-hotplug.rules. To do so, open a terminal and type gksudo gedit /etc/udev/rules.d/90-monitor-hotplug.rules. The gksudo is needed, because only root may create files in this directoy. Then paste the following line in the file and save it:

SUBSYSTEM=="drm", ACTION=="change", RUN+="/bin/su user -c '/home/user/bin/monitorhotplug.sh'"

Of course, you have to substitute the word user with your own username. This will cause udev to run the command specified behind RUN. The command executes the file /home/user/bin/monitorhotplug.sh as your user. This file has now to be created.

To to so, open a terminal and type gedit /home/user/bin/monitorhotplug.sh. Remeber to substitute user with your own username. Now paste the following script into the file and save it:

#!/bin/bash

# we need some environment variables
export DISPLAY=:0
export XAUTHORITY=~/.Xauthority
eval $(sed -n '/^#/! { s/^/export /; s/$/;/; p }' ~/.dbus/session-bus/*-${DISPLAY//:/})

# if a VGA monitor is plugged in:
if [ $(cat /sys/class/drm/card0-VGA-1/status) = "connected" ]; then
    # set my power settings
   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'
    xrandr --output VGA1 --auto
    # turn off laptop screen
    xrandr --output LVDS1 --off
# in case of anything else reset to standard options
else
   # reset my power settings
   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'
   # xrandr does the rest alone
    #xrandr --output VGA1 --off
    #xrandr --output LVDS1 --auto
fi

Now the file has to be made executable. To do so, open a terminal and type chmod +x /home/user/bin/monitorhotplug.sh.

Now it should work: 1. When you plug in a VGA monitor, your internal Laptop Screen (LVDS) will be turned off and your laptop will not suspend, if you close the lid. 2. When you plug the monitor out, or any other event with monitors occures, it will reset the power settings to suspend when the lid is closed.

There is still one little issue: When you have your monitor plugged in and close the lid, ubuntu will lock the screen, so you have to move the mouse and enter your password again.

I hope this helps somebody. Me, I am personnaly enjoying my little DIY docking station. :)

1
  • 1
    This great answer worked for me almost out of the box. I had to make two changes, and I summarize here in case someone does not get it working. 1) my laptop display was called actually eDP1 instead of LDVS1 (you can check this with xrand), 2) my account did not have permissions for ~/.dbus folder, so I had to do sudo chown user -R ~/.dbus in order to make it work.
    – HYS
    Oct 29, 2015 at 17:10

You must log in to answer this question.

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