2 # A wrapper which runs some Perl script or command as a service
4 BEGIN { open(ERR, ">c:/temp/win32.err");
5 print ERR "Starting ..\n"; };
9 # Tell the OS to start processing the service...
10 Win32::Daemon::StartService();
12 # Note: Added for convenience: The numeric codes for the Windows
15 # SERVICE_NOT_READY = 0
17 # SERVICE_START_PENDING = 2
18 # SERVICE_STOP_PENDING = 3
20 # SERVICE_CONTINUE_PENDING = 5
21 # SERVICE_PAUSE_PENDING = 6
23 # Wait until the service manager is ready for us to continue...
25 while( SERVICE_START_PENDING != Win32::Daemon::State() ) {
29 # Now let the service manager know that we are running...
30 # This needs to be here, not after the client process exits,
31 # otherwise the service will be in SERVICE_START_PENDING when
33 Win32::Daemon::State( SERVICE_RUNNING );
35 # Added (CRH): We need to replace the forward slashes with double
36 # backslashes only in the first argument to the function. For some
37 # reason the service manager expects double backslashes.
40 unshift @ARGV, $argone;
42 # Start the program in a sub-process
43 %before = map { $_, 1 } &get_procs();
51 print ERR "pid = $pid\n";
52 @after = &get_procs();
53 @new = grep { !$before{$_} } @after;
59 if (Win32::Daemon::State() == SERVICE_STOP_PENDING ||
60 Win32::Daemon::State() == SERVICE_CONTROL_SHUTDOWN) {
63 print ERR "Killing process $p\n";
64 system("process.exe -k $p");
70 # Tell the OS that the service is terminating...
71 Win32::Daemon::StopService();
73 # Returns a list of process IDs
77 open(PROC, "process.exe |");
79 if (/^\s*(\S+)\s+(\d+)\s+(\d+)/) {