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

Anyone know how to shut down a windows perl script with the Ctrl+C signal, WITHOUT doing it from the dos window???

Ideally I'd like to be able to make the script shut down without having to have it listening out for the signal.
It already reacts properly to the signal, ie. it cleans up any files it has open and shuts down.
I just want it be able to be shut down from another perl script, without the originals dos window being open...

Any ideas???
Thanks.

Replies are listed 'Best First'.
Re: Shutting down a windows perl script?
by talexb (Chancellor) on Nov 29, 2002 at 21:30 UTC

    A technique I'm using right now is for the script to check to see if a file called StopNow exists. If so, it does whatever cleanup it has to do, and exits. It's not horribly clever but it works fine. (I suppose I could even get the script to delete the StopNow file so that it doesn't exit immediately the next time it runs.)

    Well, you did ask for 'any' ideas.

    --t. alex
    but my friends call me T.
Re: Shutting down a windows perl script?
by BrowserUk (Patriarch) on Nov 30, 2002 at 01:54 UTC

    If the process has an identifiable name, then you could look up the process id from the name using Win32::PerfLib.

    This is a simple "process list" script that may or may not help. The api is somewhat confusing at first due to the sparse documentation for the module and the complexity of the underlying api.

    Once you have the proc_id you could use Win32::Process::KillProcess($pid, $exitcode) Though whether this is any better (or even any different) to using Perl's built in kill I'm not sure.

    Alternatively, if you can modify the script that you want to kill, you could have it create and periodically check the state of a named Win32::Event. The all your kill script needs to do is open that named event$event = Win32::Event->open($name), and then set it $event->set. Then next time the condemned script checks the state of the Event $event->wait([$timeout]), it can commit hari-kari by whatever method you select.

    Of course this is analogous to the Perl signaling mechanism. I have wondered why AS never implemented signals this way under the covers, but there is probably a good reason.


    Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
    Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
    Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
    Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.

Re: Shutting down a windows perl script?
by chromatic (Archbishop) on Nov 29, 2002 at 22:02 UTC

    Use kill. You may have to write the pid of the victim to a file, as many Unix utilities do. Also be careful that you need appropriate permissions.

    Update: er, yeah, Windows. I think it handles SIGINT, but I'm not positive.

Re: Shutting down a windows perl script?
by talwyn (Monk) on Nov 30, 2002 at 00:41 UTC
    Maybe you could look for the window, get its handle and send a WM_CLOSE signal to it?
Re: Shutting down a windows perl script?
by buzzthebuzzsaw (Acolyte) on Dec 01, 2002 at 22:24 UTC
    Hi all,
    I went and tried talexb's idea, and it runs a treat.
    There probably is a more elegant way of doing this but I thought that I'd use this, it's nice and simple, does the trick, and simply, that I'm kicking myself that I never thought of it!!!

    Thank's a million, guys.