in reply to print on reboot or process shutdown

Read about END blocks in perlmod.

Replies are listed 'Best First'.
Re^2: print on reboot or process shutdown
by marto9 (Beadle) on Jul 29, 2008 at 21:18 UTC
    Ty. I read the doc and was able to fix it. This is the code that I place in the beginning of my script.
    END { open(THEND,">ABORTED"); close(THEND); }
    But the END block is also executed when the script finishes. And it should only be executed on reboot or process kill.
      If you only have one normal exit point to your script, have it set a flag. Check this flag in your END block, and only write to the file when it is not set.

      If you have more than one normal exit point to your script, well, refactor a bit. :-)

      PS: note that the way you're opening your file, its contents will be overwritten every time, rather than appended to. Use ">>" if you prefer the other way.

      You can use a signal handler to cacth SIGTERM

      $SIG{TERM}=sub { #do something here };

      But that cannot trap a SIGKILL so if you or your system make a kill -9, it will not be intercepted. And, no, AFAIK there is no way to intercept a SIGKILL because it should be the last resort to kill a not responding process.

      Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."