man signal.h indicates there's no SIGEXIT in Solaris 11. How can I trap it in my shell scripts? Or how can I simulate the old behavior of traping SIGEXT?
How do I trap EXIT in a solaris 11 shell script?
976 Views Asked by user2730089 At
2
There are 2 best solutions below
0

Why are you looking to the C API manual for a shell feature ?
You definitely can trap the SIGEXIT signal under Solaris 11 shells (at least ksh93, bash and sh).
$ cat /etc/release
Oracle Solaris 11.1 X86
Copyright (c) 1983, 2012, Oracle and/or its affiliates. All rights reserved.
Assembled 19 September 2012
$ cat /tmp/z
#!/bin/ksh
trap "date" exit
sleep 60
echo done
$ /tmp/z
^CThursday, August 29, 2013 10:18:58 PM CEST
$
To clarify, there is not and has never been a signal 0 or SIGEXIT under Unix. It is a "pseudo" signal that can be used in two ways:
by sending it (eg:
kill -0 pid
) to a process, in which case nothing is ever received by the target process but the sender will know if the process actually exists by checking the kill return value.by trapping it in a shell script, in which case the handler will be executed when the script exits no matter what.
To run cleanup and other similar tasks, you could wrap your script in a second script. This second script can execute the first script, store the exit code, perform cleanup, and exit with the stored code.