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?
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: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 typegksudo gedit /etc/udev/rules.d/90-monitor-hotplug.rules
. Thegksudo
is needed, because only root may create files in this directoy. Then paste the following line in the file and save it:Of course, you have to substitute the word
user
with your own username. This will cause udev to run the command specified behindRUN
. 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 substituteuser
with your own username. Now paste the following script into the file and save it: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. :)