[HOWTO] Run scripts for laptop lid open/close and dock/undock events

What this little tutorial shows is:
1. How to catch the lid open and close events.
2. How to catch the laptop dock and undock events.
3. How to run a script for each of those events.

I will show a specific example which consist in:
- When the lid closes -> play a "closing" sound and set my Pidgin status to away with a message saying "My laptop lid is closed..."
- When the lid is opened -> play an "opening" sound and set my Pidgin status to available with a message saying "I am here..."
- When I undock my laptop from the docking station -> play a "undock" sound
- When I dock my laptop into the docking station -> play a "docked" sound

NOTES:
- This was tested on a Lenovo Thinkpad T61 and a T61p both running Ubuntu 8.10. Things might slightly change depending on the laptop or linux distribution. But the main idea will be the same. This tutorial is just to point people towards the right solution.
- 20/07/09: I just tested this on a T61p with Ubuntu 9.04 and it work fine, there is one minor thing to look, stated below.
- Make sure throughout the tutorial you change your_user with your actual user on the scripts and also feel free to save scripts on different paths updating the paths on the scripts of course.
- 27/02/10: If you want to implement the lid solution on a laptop with multiple users then look at posts 32, 33 and 40 from airtonix. I haven't tested these myself.
- 14/04/10: Tested this on a Thikpad W500 with Ubuntu 9.10. The script part works fine, the dock part needs a slight change, see post 48 for details.
- 15/07/10: 14/04/10: Tested this on a Thikpad W500 with Ubuntu 10.04. The above change is also necessary.

***I am not responsible if you mess things up, please be careful!***

It is important to understand that these events will be catched by processes owned by root. So we need to do a little hack so that root can run commands on our user's X environment. Many thanks to Earl Ruby for pointing me on how to do this http://earlruby.org/2008/08/update-p...us-using-cron/

Now put it on your startup scripts. System -> Preferences -> Sessions and click Add.

Code:

Name: X Environment Variables
Command: /home/your_user/export_x_info

Click Add and close.

This will execute every time you start your computer and the call to source ~/.Xdbus loads the DBUS_SESSION_BUS_ADDRESS and XAUTHORITY environment variables before executing the purple-remote command for Pidgin.

Ubuntu makes it easy to catch the laptop lid open and close events. There is a file /etc/acpi/lid.sh which runs every time your lid open or closes. So run:

Code:

gksudo gedit /etc/acpi/lid.sh

and right after line #!/bin/bash paste /home/your_user/lid_event

Save and close.

Now create the file that will call different scripts according to open or close events.

Now you can test that it works by opening and closing your laptop lid.
Notes:
- Make sure that the gnome power management option for laptop lid closed is set to do nothing.
- The sounds used are from OpenOffice 3.0, the path changes for previous versions.
- Make sure you have libpurple-bin installed -- sudo apt-get install libpurple-bin

NOTE: For some reason on Ubuntu 9.04 the above command will not work, the file will not be made executable. The workaround is to become root and execute the chmod command.

Now lets create the undocked file:

Code:

gedit ~/undocked

and paste the following:

Code:

#!/bin/bash
#This runs so that root can run the following command under the user's environment
source /home/your_user/.Xdbus
#play a sound
DISPLAY=:0.0 su your_user -c "aplay /usr/lib/openoffice/basis3.0/share/gallery/sounds/falling.wav"

Save and close. Make it executable:

Code:

chmod +x ~/undocked

then the docked file:

Code:

gedit ~/docked

and paste the following:

Code:

#!/bin/bash
#This runs so that root can run the following command under the user's environment
source /home/your_user/.Xdbus
#play a sound
DISPLAY=:0.0 su your_user -c "aplay /usr/lib/openoffice/basis3.0/share/gallery/sounds/sparcle.wav"

Save and close. Make it executable:

Code:

chmod +x ~/docked

Now finally we need to restart hal:

Code:

sudo /etc/init.d/hal restart

That's it, you should be able to dock and undock and hear a sound each time you do. From here the options for you are endless, you can basically do anything you want.

Last edited by lunatico; July 15th, 2010 at 01:52 PM.
Reason: New multi-user lid implementation by airtonix.

Hey, thanks for the guide, honestly i think this is what i was looking for, it just didnt work for me.

A brief outline of my problem...

i have a thinkpad x61 tablet who's screen i would like to rotate when i rotate my lid. I have a button on my laptop screen that executes a program I wrote to rotate the screen. If i rotate the screen using this method, all is fine... its just inconvenient... I'd like acpi to do it for me....

I used acpi_listen to find the rotate event, created a file in /etc/acpi/events/ called "ibm-clockwise.sh". Then I made this file executable and had it call a script in /etc/acpi called "ibm-clockwise". This file is also executable. If i just put "rotate" (the name of my program) in "ibm-clockwise", compiz crashes on rotation.

So I did some testing and found that when I execute rotate as root, the same behavior is observed, i assume this is the problem, since the scripts in /etc/acpi are executed as root.

I screwed around with "su nick -c rotate" for a while, both while logged in as root, using "sudo su", and placing this command in the /etc/acpi/ibm-clockwise file. That was fun, all time wasting aside.

Everytime compiz crashes, I lose half of my workspaces... All of my special keys are unbound, and obviously desktop effects are gone... I have to go to the fusion-icon and do "reload window manager" ...

it seems this problem could be fixed by having acpi execute rotate as my user (nick). This brings me to your guide.

I followed parts of it, but i didnt really want my home directory cluttered with a bunch of scripts.

I created ~/export_x_info and pasted your info in directly. Then i made it executable. I also added it to my startup using your method, but I didnt feel like restarting for it to take effect, so i executed it with a ./export_x_info ...

I skipped all the linking and just went directly to pasting the source ~/.Xdbus stuff into the action script since i only need it to do one thing... my /etc/acpi/ibm-clockwise file looks like

#!/bin/bash
source /home/nick/.Xdbus
DISPLAY=:0.0 su nick -c "rotate"

Though im not even sure if the "DISPLAY=:0.0" is necessary, i tried removing it and things still didnt work.

i restarted acpi and tested, and compiz still crashes, i assume because the file is still not being executed as my user (nick).

First, for troubleshooting purposes I recommend running stuff yourself as root. This way you will determine when, where and why it is not working. The idea is something like:

"su -" and:

Code:

env | grep DBUS_SESSION_BUS_ADDRESS
env | grep XAUTHORITY

Probably this didn't returned anything. That's fine. If you run your "rotate" script now it will most likely crash as you described. Try and see. You probably will see where it is failing.

Now:

Code:

source /home/daniel/.Xdbus

And again:

Code:

env | grep DBUS_SESSION_BUS_ADDRESS
env | grep XAUTHORITY

This time you should get your session's values. So now you should be able to start running stuff from root as your user on your current session.
What I suspect is happening is that there is a lot of stuff inside your rotate script that is still just been run as root. See, what happens is that when you do "source /home/nick/.Xdbus" the env variables are been imported just to that environment, if you run a script from there you are creating a new bash environment just for that script.
You should call "source /home/nick/.Xdbus" again within your rotate script and stuff that you do there that uses your session's env variables should be run as DISPLAY=:0.0 su nick -c "etc....."
Do you see my point?

the output is the same both times, the first returns nothing, the second returns ~/.Xauthority

I tried rotating the screen before and after both of those blocks... compiz crashed all 4 times... the output of my rotate program was pretty much the same in each case... the only instruction that really returns anything is the

to my rotate program and then executing anything that uses the env variables using su nick -c <do-stuff> ... I don't know which statements pertain to the env variables... how do i identify them (im lost needless to say)

at this point i think I've narrowed down the segment of code in my program that is causing all the problems. when i comment out this line, the screen rotates just fine, but the keys that i need reassigned after rotation aren't recognized (which is why it's there). But this is the troublesome line.

system(compiz --replace);

So i need to somehow give root the ability to restart compiz... playing with this idea, i started issuing compiz --replace every odd way i could think of (after assigning the environment variables) which didnt work =(

Well, first thing is just to say that "sudo su" it's not the same as "su -". They both make you root alright but with the first you are still on your user's home directory, whereas the second makes you root and places you on root's home directory. That's why you are getting something for your xauthority value but not for your bdus session value.
When running scripts as root it is on root's home directory. So if you just call your "rotate" script root is not even able to find it. Probably the first problem you have in the line. You should use the full path of your script.

Originally Posted by e64462

But this is the troublesome line.

system(compiz --replace);
any ideas?

It sure is a troublesome line. I just tried

Code:

DISPLAY=:0.0 su user -c "compiz --replace"

And it crashed....
Compiz is a very complicated piece... you may want to ask on their forums to see what you need to do to restart it as root but for your users session. Sorry I can't be of more help.