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

Handling killing the perl process

by ohadhawk (Initiate)
on Jan 04, 2010 at 09:09 UTC ( #815542=perlquestion: print w/replies, xml ) Need Help??
ohadhawk has asked for the wisdom of the Perl Monks concerning the following question:

Hello dear monks,

My perl script performs some actions that may leave the system in an inconsistent state.
What I'd like to do is either roll back some actions on exit or notify the user running the script to do it manually.

I've assigned my own handlers to all the applicable termination signals, such as INT, ABRT, KILL, etc' and this works.

But the problem is that I don't know how to handle termination of the process by windows - either by the task manager or by closing the console window.

Does any wise monk know of a way for me to be able to catch the termination of the process ?

May light shine on your path :-)

Replies are listed 'Best First'.
Re: Handling killing the perl process
by afoken (Abbot) on Jan 04, 2010 at 10:01 UTC

    Microsoft Windows has no concept of signals, we've discussed that in $SIG{ALRM} and windows vista? and some other threads. Perl implements an emulation, but it is not perfect, and it probably won't help you here.

    Windows sends lots of messages to a window when it thinks the process owning the window should terminate, like WM_CLOSE and WM_QUIT. So you need to catch those messages. With a console window, things are a little bit more complicated, as Windows fakes some keypresses (from memory - RTFM), too, and you can't catch the messages sent to the console window (from memory - RTFM). And ultimately, Windows can forcefully terminate a process by directly invoking ExitProcess() or the like from the processes' context, without any way for the process to stop or delay that (similar to kill -9, but implemented as "forced suicide").


    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://815542]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2018-06-24 13:23 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.