Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2015-07-28 01:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (251 votes), past polls