0

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?

Jolta
  • 2,620
  • 1
  • 29
  • 42
  • 1
    how could you trap that which doesn't exist? It'd be like putting traps for Bigfoot. You'll catch lots of other things, but no bigfoots. – Marc B Aug 29 '13 at 16:30
  • Looks like you're trying to force fit a particular solution instead of looking for an answer to the actual problem. What is it that you're trying to accomplish by trapping SIGEXIT? – Sir Athos Aug 29 '13 at 17:07
  • I want to run commands no matter how the shell script exits. It's a pretty common practice in writing shell scripts. Cleaning up tempfiles and the like. – user2730089 Aug 29 '13 at 18:25
  • Hack your shell to simulate a signal? – tripleee Aug 29 '13 at 19:30

2 Answers2

3

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.

jlliagre
  • 29,783
  • 6
  • 61
  • 72
0

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.

Sir Athos
  • 9,403
  • 2
  • 22
  • 23
  • I could do that, but that seems like a huge step backwards given that trapping exit has existed for(ever?) a really long time. Also, how would the wrapper script know about the state of things in the "inside" script? (what temp files were opened, etc) – user2730089 Aug 29 '13 at 18:46
  • From what I'm reading, Solaris 10 doesn't have this signal either, so it was taken out a long(er) time ago as well. It is a step backwards, but it's a working starting point. You could also (in some cases) modify your script to avoid exiting on error. As for passing the state, you can use a "master temp file" to save state, the name of which you pass as an argument from the wrapper script to the wrapped script. – Sir Athos Aug 29 '13 at 18:49
  • 1
    This signal wasn't taken out a long time ago, it just has never existed in the first place. It is a pseudo (i.e. fake) signal. – jlliagre Aug 30 '13 at 10:53