BEGIN { open( STDERR, ">>D:/htdocs/myservicelog.err" ) or die "invisible error"; warn "$0 started ".localtime().$/; } #### complicatedtask.pl started Mon Dec 27 14:38:24 2004 etc. #### open LOG, ">>mylogfile.txt" or die; # Start the service control loop Win32::Daemon::StartService(); while( SERVICE_STOPPED != ( $State = Win32::Daemon::State() ) ){ if( SERVICE_STARTING == $State ) { # some service startup code ...... Win32::Daemon::State( SERVICE_RUNNING ); } elsif( SERVICE_PAUSE_PENDING == $State ) { Win32::Daemon::State( SERVICE_PAUSED ); next; } elsif( SERVICE_CONTINUE_PENDING == $State ) { Win32::Daemon::State( SERVICE_RUNNING ); next; } elsif( SERVICE_STOP_PENDING == $State ) { # some service shutdown code ............ Win32::Daemon::State( SERVICE_STOPPED ); next; } elsif( SERVICE_CONTROL_SHUTDOWN == $State ) { # Request x seconds to shutdown... Win32::Daemon::State( SERVICE_STOP_PENDING, 45 ); .............. Win32::Daemon::State( SERVICE_STOPPED ); next; } elsif( SERVICE_RUNNING == $State ) { doMyStuff(); sleep 30; } else { # Un-handled control messages Win32::Daemon::State( SERVICE_RUNNING ); } sleep 5; } Win32::Daemon::StopService(); sub doMyStuff { print LOG "hello\n"; }