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

Hello,
I have code that uses WMI NextEvent to monitor event log activity on Windows systems.

I am useing Activestate Perl Service module (PerlSvc from thier Developers kit) to make it into a service. The servce runs fine but the service will not stop normamly. You have to manualy kill the process PID.

Active State Tech Support said: "It looks then like NextEvent is blocking in such a way that it doesn't respond to the service control commands, at least with your implementation."

The service stops OK if I use the NT Resource kit SRVANY, but I'd rather not use SRVANY because it is a seperate process and not as reliable as a perl based soloution.

I'd would appreciate any ideas to get this working or alternate methods that do not use the Active State perlsvc. Following is sample test code.

package PerlSvc; our %Config = (ServiceName => "TestService"); require Getopt::Long; $PerlSvc::Verbose = 0; # Put PerlSvc into quiet mode sub Startup { Getopt::Long::GetOptions('interval=s' => \$interval); #main::Init(); while(ContinueRun()) { # $ main::testservice(); # } } sub Interactive { # this callback is invoked when your service is not running as # a service and has been called without any of the --help, # --install or --remove options. Install(); Startup(); } sub Pause { # your service is about to be suspended } sub Continue { # your service will resume execution now } sub Install { Getopt::Long::GetOptions( 'interval=s' => \$interval, ); $Config{ServiceName} = "TestService"; $Config{DisplayName} = "TestService"; $Config{Description} = "TestService"; # add your additional install messages or functions here } sub Remove { # add your additional remove messages or functions here } sub Help { # add your additional help messages or functions here } package main; sub testservice { ################################################ Begin Program # +################################################# use Win32::OLE qw(in); my $evtQuery = "SELECT * FROM __instancecreationevent WHERE targetinst +ance ISA 'Win32_NTLogEvent' and (targetInstance.EventIdentifier= '592 +')"; my $Events = Win32::OLE->GetObject("WinMgmts:{impersonationLevel=imper +sonate,(security)}")-> ExecNotificationQuery($evtQuery) || warn + Win32::OLE->LastError; while (my $Event = $Events->NextEvent) { open (LOG, ">>testservice.log") || die "Could not Open testservice +.log"; print LOG ((localtime)." A new Process has been created\n"); close (LOG); return unless PerlSvc::ContinueRun(); } ############################################### END Program ##### +############################################### }

janitored by ybiC: Minor layout tweaks including balanced <readmore> tags

Replies are listed 'Best First'.
Re: Problem Stopping Service using Active State Perlsvc
by gellyfish (Monsignor) on Jun 23, 2005 at 14:16 UTC

    I don't know about PerlSvc at all but when creating a service in any other language you have to provide an implementaton of the stop method - perhaps adding a

    sub Stop { }
    might help.

    /J\

Re: Problem Stopping Service using Active State Perlsvc
by Jenda (Abbot) on Jun 23, 2005 at 22:17 UTC

    After about 15 minutes of searching I found the docs of the NextEvent() method (yeah, maybe someone would find it sooner, I guess I'm not twised enough). The method accepts a timeout. What you need to do is to specify some timeout (1 second seems enough to me) so that you do call the ContinueRun() occasionaly. This of course means that you now have to check the $Event and write something to the log only if the ->NextEvent() did not return because of the timeout.

    Jenda
    XML sucks. Badly. SOAP on the other hand is the most powerfull vacuum pump ever invented.