BenHopkins has asked for the wisdom of the Perl Monks concerning the following question:

Is there a sig vector that fires when a process terminates normally? I'm working on a module that (among other things) writes to a log file. The first time it's called, it opens the log file, but how can you tell when it's the last time?

I'd like to do something like $SIG{'TERM'} = \&clean_up;. In fact, I did exactly that, but it didn't work.

Is there another signal?

Replies are listed 'Best First'.
Re: handler for normal termination?
by almut (Canon) on Jun 23, 2007 at 01:55 UTC

    Maybe you want an END { ... } block?   (see perlmod for details)

      That is it. It is a perl thing, not a unix thing. Thanks.
Re: handler for normal termination? (END)
by tye (Sage) on Jun 23, 2007 at 01:57 UTC

    No, signals aren't sent for normal flow.

    END { clean_up() }

    Note that you can use $? in an END handler to get a guess as to whether the script is ending "normally" or not.

    - tye        

      C always cleaned up after termination using, I figured, a signal from UNIX on process termination. Silly me to think that behavior would be emulated. Anyhow, the END block works just fine. Thanks.