Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: print on reboot or process shutdown

by gaal (Parson)
on Jul 29, 2008 at 21:07 UTC ( #700925=note: print w/ replies, xml ) Need Help??

in reply to print on reboot or process shutdown

Read about END blocks in perlmod.

Comment on Re: print on reboot or process shutdown
Download Code
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.

      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."

      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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://700925]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (16)
As of 2015-03-27 14:15 GMT
Find Nodes?
    Voting Booth?

    When putting a smiley right before a closing parenthesis, do you:

    Results (608 votes), past polls