1835084 Members
3509 Online
110073 Solutions
New Discussion

capture exit staus

 
SOLVED
Go to solution
Donny Jekels
Respected Contributor

capture exit staus

how do I capture a a user exit/logout and perform various tasks,

hmm....
like clear out temp files for the user,
unset VAR's
run different command in the background
etc.
in KSH, for csh users I use .logout
"Vision, is the art of seeing the invisible"
8 REPLIES 8
James A. Donovan
Honored Contributor
Solution

Re: capture exit staus

you could try using the trap command.

Try placing this as the last line in the users' .profile

trap "some_cleanup_script" 0

see the man page for ksh for more details
Remember, wherever you go, there you are...
Donny Jekels
Respected Contributor

Re: capture exit staus

trap,

tell me more about this trap thing
"Vision, is the art of seeing the invisible"
Bill Hassell
Honored Contributor

Re: capture exit staus

You'll want to look at the trap command where you can define the action to be taken under various conditions. For instance, cleaning up temp files whenever the shell exits:

set -u
export PATH=/usr/bin
export MYNAME=$(uname -n)
export TEMPPATH=/var/tmp/$MYNAME.$$
export TEMPFILE1=$TEMPPATH/temp1

trap 'rm -f $TEMPPATH' 0 1 2 3 15

So: set -u prevents undefine variables from being used (can be a problem with misspelled variables)

PATH is redfined inside the shell script to prevent improper $PATH locations from being used.

TEMPPATH specifies a directory for all temp files that is unique to this specific shell script.

TEMPFILE1 defines one (of possbily many) temp files.

trap defines what action to take if the script terminates for any reason (including a normal exit, trap ... 0). You can create multiple traps for each signal (ie, kill -15, etc).

Now if this is an action to be taken in the login shell, then put the:

trap 'series of tasks' 0

in .profile for each user. Be sure to put the trap near the top of the script so it will be effective even during .profile's execution. NOTE: there is no need to unset VAR's when a shell exits, whether it is the login shell or a shell script. These VARs are stored in the shell's data area so when teh shell exits, everything is returned to the kernel for reuse. AS far as temp files for each user, that will be quite difficult to code since users might ctreate temp files anywhere they can write.

trap is used in /etc/profile to PREVENT the normal action for a kill signal such as CTRL-c (ie, kill -15). The trap command in /etc/profile at the top of the script essentially says: ignore all unexpected kill signals (kill -9 cannot be trapped) and do nothing. The trap at the end of /etc/profile is REQUIRED to turn normal behavior for these trap signals.

The trap commnand is part of all POSIX shells such as ksh and HP's POSIX shell (and bash, etc).


Bill Hassell, sysadmin
Donny Jekels
Respected Contributor

Re: capture exit staus

if I build a function containing a series of tasks,
can I use

trap '' 0 1 2 3 15

??
"Vision, is the art of seeing the invisible"
Tony Contratto
Respected Contributor

Re: capture exit staus

Yes, You can use a function.
got root?
S.K. Chan
Honored Contributor

Re: capture exit staus

Yes you call a function in trap .. one example that I've quickly tested ..

#!/bin/ksh

trap delem 0 1 2 3 15

TMPFILE=/tmp/myfile.$$

touch $TMPFILE

function delem {
date; rm -rf $TMPFILE 2>/dev/null; exit 1
}
Donny Jekels
Respected Contributor

Re: capture exit staus

thanx, guys!

I am okay from here on now.

what is the CTRL C code if I want to watch for it? to not break the script??
"Vision, is the art of seeing the invisible"
Bill Hassell
Honored Contributor

Re: capture exit staus

CTRL-c is the key combination that is used to terminate a process or a script while it is running. Technically, it generates a kill -15 to the currently running process on the terminal. So if you run sleep for instance:

sleep 10

then press the CTRL key and also press c at the same time, sleep will be terminated. The trap command senses this condition and takes the action specified in the trap statement. The other numbers given in the above examples (0 1 2 3 15) are other kill sugnals. To get a quick reference, type:

kill -l

and look at the man pages for:

man kill
man signal


Bill Hassell, sysadmin