Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Exiting batch-started Perl script on WinXP shutdown

by gri6507 (Deacon)
on Apr 17, 2012 at 12:24 UTC ( #965500=perlquestion: print w/ replies, xml ) Need Help??
gri6507 has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks,

I wrote a script which I start with a batch file in the Startup directory using a batch file similar to
@c:\path\to\perl.exe c:\path\to\myscript.pl
The ampersand suppresses the CMD windows from showing up as an empty black box, making my Perl script run in the background. Because my script does something like

while(1){ DoSomething(); sleep(600); }

my script remains always alive. The problem is that when I then try to shutdown windows, the shutdown is first delayed for about 10 seconds, then Windows complains that CMD.exe is not responding, and I have to manually kill the process by pressing the End Process button.

Are there any special tricks for terminating Perl scripts on Windowns shutdown?

Comment on Exiting batch-started Perl script on WinXP shutdown
Select or Download Code
Re: Exiting batch-started Perl script on WinXP shutdown
by brx (Pilgrim) on Apr 17, 2012 at 13:03 UTC

      You may even look at a wrapper of that module: Win32::Daemon::Simple. It'll take care of installation/deinstalation, logging, will store settings in registry for you allowing you to change them by calling the script with the right parameters, do the looping for you in such a way that the program responds to the service manager's requests in a timely matter, etc.

      The only catch is that you have to install a patch to Win32::Registry from http://jenda.krynicky.cz. (It was once supposed to replace the Win32::Registry on CPAN & within libwin32, but that somehow never happened.)

      Jenda
      Enoch was right!
      Enjoy the last years of Rome.

Re: Exiting batch-started Perl script on WinXP shutdown
by BillKSmith (Chaplain) on Apr 17, 2012 at 13:21 UTC
    I would run DoSomething as a windows scheduled task. On XP, you can access the scheduler as Start->Programs->Accessories->System Tools->Scheduled Tasks
Re: Exiting batch-started Perl script on WinXP shutdown
by rovf (Priest) on Apr 17, 2012 at 14:34 UTC

    Do you also see this behaviour, when you remove the DoSomething? I also have Perl programs running from the command line, but so far they didn't cause trouble when shutting down Windows.

    What happens if you just kill the command window, within which the Perl process runs? Is the Perl process still alive afterwards (you can use Process Explorer to find out; it's more comfortable to use than the Windows Task Manager).

    As a minor issue, you don't have an ampersand in your line, and if you had one, it would, AFIK, not make your command running in background on Windoze (unless, of course, you would have something like Cygwin and use one of their shells).
    -- 
    Ronald Fischer <ynnor@mm.st>
Re: Exiting batch-started Perl script on WinXP shutdown
by SuicideJunkie (Priest) on Apr 17, 2012 at 15:22 UTC

    Shouldn't the script be receiving a signal when windows shuts down?

    Using %SIG to set an appropriate action (cleanup; exit 0) might be the simplest way to go about this.

      It looks like p5git://win32/win32.c. turns CTRL_SHUTDOWN_EVENT into a SIGTERM, so $SIG{TERM} should work for this case.

      - tye        

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://965500]
Approved by ww
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2014-12-26 03:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (164 votes), past polls