There are a few ways to do this. Ubuntu's graphical login is provided by GDM (or KDM if you're using Kubuntu). GDM is started by the Upstart subsystem.
The startup process follows these steps:
- System boots. Upstart starts services, including GDM (/etc/init/gdm).
- GDM starts, initializes the X-server (/etc/gdm/Init/*), and presents the GUI login window.
- A user logs in.
- PAM authorization happens (/etc/pam.d/gdm)
- GDM runs PostLogin script (/etc/gdm/PostLogin/*).
- GDM runs PreSession script (/etc/gdm/PreSession/*).
- GDM runs Xsession and xinit scripts
(/etc/gdm/Xsession, /etc/X11/xinit/xinitrc.d/*, /etc/X11/Xsession, /etc/X11/Xsession.d/*)
- The user's desktop appears.
Where to run your script
Everything prior to XSession runs as root. /etc/gdm/Xsession and everything after runs as the user. This leaves you with three real options for where to run your script.
Modify GDM/KDM's PostLogin or PreSession scripts to run your program. The username is available in the USER or USERNAME environment variables.
Use PAM to execute your script. PAM will set the authorizing user in the PAM_USER environment variable. Add this to /etc/pam.d/gdm to kick off your script:
auth required pam_exec.so /path/to/your/script
- You might be able to use PAM to match a particular user (as in this answer), so the script would only run for that user and wouldn't need to match users itself. I don't have the PAM expertise to explain how to do that.
Write an Upstart script to run your program. Your script would start at user login, so we look for the desktop-session-start signal emitted by GDM's PreSession script.
So an Upstart script would detect that signal as the run trigger:
# start when GDM's PreSession script runs
start on desktop-session-start
The signal from PreSession doesn't pass along the username, so you'd need to tweak the signal. In /etc/gdm/PreSession/Default, find the initctl line and change it to this. You could also use USERNAME in place of USER.
# add USER variable so Upstart script can find it
initctl -q emit desktop-session-start DISPLAY_MANAGER=gdm USER=$USER
See the manpages for Upstart and its starting event for more details.
How to avoid the Admin user
Your script will need to examine the user/username in the environment variables it gets from one of these methods and use that to determine whether to abort or continue. Standard shell-scripting methods will work. Depending on which starting location you chose from the above list, the username may be available in the USER, USERNAME, or PAM_USER environment variables.
that works if it's a program the user wants to run. i don't think that's what this questioner is looking for tho. – quack quixote – 2010-04-25T05:14:42.013