use Win32::Daemon; $SERVICE_SLEEP_TIME = 20; Win32::Daemon::StartService(); sleep (1) while(SERVICE_START_PENDING != Win32::Daemon::State()); Win32::Daemon::State(SERVICE_RUNNING); $dontStop = 1; $count=0; # Note: the $count stuff is here in case i can't stop it.. purely for debugging while (($dontStop) && ($count++ < 600)) { # Process stuff here open(OUT,">>e:\\temp3\\testout.txt"); print OUT localtime(); print OUT " $_\n" for (1..10); close(OUT); if(SERVICE_CONTROL_NONE != ($Message = Win32::Daemon::QueryLastMessage())) { if(SERVICE_CONTROL_INTERROGATE == $Message) { Win32::Daemon::State($PrevState); } elsif(SERVICE_CONTROL_STOP == $Message) { Win32::Daemon::State(SERVICE_STOP_PENDING, 25000); $dontStop = 0; } } Win32::Sleep($SERVICE_SLEEP_TIME); } Win32::Daemon::StopService();