Sunday 14 April 2013

How to execute a script for each logout [Shell Script]

Sometimes it's useful to execute a command after each logout, i.e. to cleanup temporary directories, or to log working hours.

This is the way to make the shell execute the script $HOME/.atexit after each logout:

1. Insert this line at the beginning of the file

$HOME/.profile:
trap ". $HOME/.atexit" 0

2. Create a file named "$HOME/.atexit"

it that may contain arbitrary shell commands,
i.e.
$ cat > $HOME/.atexit
# echo "Good bye $LOGNAME, the time is `date`"
^D
That's it! After the next login, the shell will execute the contents of the file "$HOME/.atexit".

NOTE: this will not work for C-Shell dialects, i.e. TCSH

0 blogger-disqus:

Post a Comment