Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

print on reboot or process shutdown

by marto9 (Beadle)
on Jul 29, 2008 at 20:35 UTC ( [id://700920]=perlquestion: print w/replies, xml ) Need Help??

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

Hello. I'd like to print a msg into a txt file if the computer reboots or the process is shutdown. The environment is windows. Ty

Replies are listed 'Best First'.
Re: print on reboot or process shutdown
by gaal (Parson) on Jul 29, 2008 at 21:07 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."

Re: print on reboot or process shutdown
by whereiskurt (Friar) on Jul 29, 2008 at 21:43 UTC

    Hello!

    Different OSes are going have slightly different signals. I found this for windows:

    • ABRT2 -- This signal means that another process is trying to abort your process.
    • BREAK2 -- This signal indicates that a Ctrl+Break key sequence was pressed under Windows.
    • TERM2 -- This signal means that another process is trying to terminate your process.
    • SEGV2 -- This signal indicates that a segment violation has taken place.
    • FPE2 -- This signal catches floating point exceptions.
    • ILL2 -- This signal indicates that an illegal instruction has been attempted.
    • INT2 -- This signal indicates that a Ctrl+C key sequence was pressed under Windows.
    • sub kill_handler { open(my $theend, ">", ABORTED"); close($theend); } $SIG{'ABRT2'} = 'kill_handler'; $SIG{'ILL2'} = 'kill_handler';

      Kurt

Re: print on reboot or process shutdown
by marto9 (Beadle) on Jul 30, 2008 at 10:40 UTC
    I've got this now.
    $endthis = 0; ... $endthis = 1; exit; END { if ($endthis == 0) { open(THEND,">ABORTED"); close(THEND); } }
    But it doesn't work well. I dunno why. And I also have another prob. die "not good!\n" && ($endthis = 1) && (unless ($ARGV[1])); What's the correct syntax for this? Thx in advance
Re: print on reboot or process shutdown
by marto9 (Beadle) on Jul 30, 2008 at 08:24 UTC
    Thx everyone for the help. I've tried the $SIG method, but it doesn't seem to work a 100%. Now I'd like to try the method from gaal. It looks good. But I can't find anything about flags on google.
Re: print on reboot or process shutdown
by Bloodnok (Vicar) on Jul 30, 2008 at 12:36 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://700920]
Approved by Joost
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-25 16:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found