The stupid question is the question not asked PerlMonks

### Exiting batch-started Perl script on WinXP shutdown

by gri6507 (Deacon)
 on Apr 17, 2012 at 12:24 UTC 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);
}
[download]

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?

Replies are listed 'Best First'.
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 (Priest) 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 SuicideJunkie (Vicar) 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 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> 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?  [shmem]: perl -lE '$_="odd mornin";($o=$_)=~ s/^|$/g/g;say$o' [Eily]: Hi monks o/ [choroba]: 3 PM in Vietnam, BTW Discipulus maybe a wrong impression.. [karlgoethebier]: Discipulus: Workshops are for weenies ;-) [Discipulus]: here 43C° high umidity, tiger mosquitos, violence in the street.. everywhere is vietnam [karlgoethebier]: OK, i'm socially unacceptable [karlgoethebier]: Discipulus: Were the hell are you today? [Discipulus]: ..or suitable for the presidence ;=) [Discipulus]: at Caput Mundi

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2017-06-27 08:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
How many monitors do you use while coding?

Results (600 votes). Check out past polls.