Download location (HTTP):       http://linux.bononline.nl/linux/mount.md5key.new/runsessionscripts/src


Run scripts when session starts en when it ends.


INTRODUCTION

Other projects I work on, like fuse-workspace and automounting with md5keys, require a construction which launch them when a session begins, and terminates when the session ends. Now ConsoleKit offers this, at documented in the source of ConsoleKit (at least 0.3.1), and Upstart.

GENERAL SETUP

To make this work create the following, to have a basic setup to run session scripts anyway:

install --directory /etc/session.d/scripts/start
install --directory /etc/session.d/scripts/stop
The general place to put start scripts in, is /etc/session.d/scripts/start. Just simular for the stop scripts.
For example a script to mount the fuse module fuse-workspace for a user goes in the start directory, and to unmount it again in the stop directory.

Helper scripts and special functions (for example logging) go in the functions map.

Running directly by ConsoleKit

ConsoleKit offers the ability to run scripts when a session is changed, added or removed, at documented in the source of ConsoleKit (at least 0.3.1):

Now my idea is to use a script in /etc/ConsoleKit/run-session.d which on his turn runs scripts it find in /etc/session.d/CK-added when a session is added, and scripts it finds in /etc/session.d/CK-removed when a session is removed.

So create this directories:

install --directory /etc/session.d/CK-added
install --directory /etc/session.d/CK-removed
and make a symlink to the desired script in /etc/session.d/scripts :

ln --symbolic --force ../scripts/start/example_start_script.sh /etc/session.d/CK-addded/
ln --symbolic --force ../scripts/stop/example_stop_script.sh /etc/session.d/CK-removed/

This start script can for example mount shares, and the stop script unmounts them.

Important to note is that there is no construction here to wait for the script to finish, and/or to check the exit status and do something with that. Scripts are just started.

Upstart is a better alternative for this.

Basically, this scripts determines the user for which the session is changed, added or removed, by looking at the environment variable CK_SESSION_USER_UID. Next it maintains a simple table in /tmp/ConsoleKit with all available sessions and users logged in. The reason for this is that I find the data presented by the CK tools not so usable for scripts, and then I mean how many users are logged in, which sessions there are, and which session belong to a user.
Next it does look if scripts should run via the at daemon, or directly. Finally, it runs all the scripts, in /etc/session.d/CK-added when a session is added and in /etc/session.d/scripts/CK-removed when a session is removed. It passes one parameter to these scripts, the userid.

When the parameter SESSION_USE_ATD is set to yes, and when the utility at is found and when the atd daemon is running, and only then, the scripts are run by the at daemon like:

echo "/etc/session.d/scripts/start/testscript $userid" >> /tmp/atscript
at -q "b" -f /tmp/atscript NOW

If not run by the at daemon, it runs them directly like:

/etc/session.d/scripts/start/testscript $userid &

Note the last & is very important, it makes run_script.ck not wait for the script to finish.

About running scripts with Upstart

I've developed this construction before I knew Upstart. Upstart is created to run scripts on specific moments, meant as replacement of the traditional init. It's also possible to make it session aware (very easy!!). I'm planning this for future, I'm not in a situation I can test Upstart now. Look at the website:

http://upstart.ubuntu.com/


and a topic about starting and stopping a session at the maillist:

https://lists.ubuntu.com/archives/upstart-devel/2009-October/001091.html



Licence

This software and the construction is distributed under the terms of the GNU General Public License. A copy you'll find at the src directory.