I am beginner in perl. Following is the sample script from http://www.roth.net/perl/Daemon that I am trying to play with. I am trying to register this script as a windows service that should run every 5 seconds. I executed the the script on the commandline and it did creat a service under windows services. When I exit the command window the service doesn't run and the task I am trying to achieve is not being done. If I go and manully try to start this service under windows services I get the following error:
Error 1053: The service did not respond to the start or control request in a timely fashion
How should I make it run every five seconds as a windows service. Any help is really appreciated.

use strict; use vars qw( %Config ); use Time::Local; use Getopt::Long; use Win32::Daemon; use Win32::OLE qw( in ); %Config = ( machine => Win32::NodeName(), logfile => "c:\\temp1\\Noname1.txt", #( Win32::GetFullPathName( $ +0 ) =~ /^(.*)\.[^.]*$/ )[0] . ".log", service_name => "TESTSERVICE", service_alias => "SERVALIAS", #user => "test", #password => "test1", # How much time do we sleep between polling the service state? # This is in milliseconds service_sleep_time => 100, # How often do we query the list of processes to determine if # there is one to kill? # This is in seconds update_proc_list_time => 5 ); # Try to open the log file if( open( LOG, ">>$Config{logfile}" ) ) { # Select the LOG filehandle... my $BackupHandle = select( LOG ); # ...then turn on autoflush (no buffering)... $| = 1; # ...then restore the previous selected I/O handle select( $BackupHandle ); Log( "Starting the $Config{service_name} service." ); Log( "PID: $$" ); Log( "Machine: " . Win32::NodeName() ); Log( "Task: Monitoring processes on machine $Config{machine}" ); } RemoveService(); InstallService(); sub InstallService { my $ServiceConfig = GetServiceConfig(); if( Win32::Daemon::CreateService( $ServiceConfig ) ) { Log( "Linking to WMI..." ); print "The $ServiceConfig->{display} was successfully installed.\n +"; # Okay, go ahead and process stuff... while(1) { # delete all *.tmp files at c:\temp unlink( glob( "c:\\temp1\\*.txt" ) ); #keep_alive(); sleep(5); # and wait #last unless keep_alive(); Log("PRINTING"); } } else { print "Failed to add the $ServiceConfig->{display} service.\nError +: " . GetError() . "\n"; } } sub Log { my( $Message ) = @_; print LOG "[" . localtime() . "] $Message\n" if( fileno( LOG ) ); } sub GetServiceConfig { my $ScriptPath = join( "", Win32::GetFullPathName( $0 ) ); my %Hash = ( name => $Config{service_name}, display => $Config{service_alias}, path => $^X, #user => $Config{user}, #pwd => $Config{password}, description => "Monitors processes and kills them after a config +ured time.", parameters => "\"$ScriptPath\" -l \"$Config{logfile}\" -n \"$Con +fig{service_name}\" -m $Config{machine}" ); # Make sure that the display name is unique... $Hash{display} .= " ($Hash{name})"; return( \%Hash ); } sub GetError { return( Win32::FormatMessage( Win32::Daemon::GetLastError() ) ); } sub RemoveService { my $ServiceConfig = GetServiceConfig(); if( Win32::Daemon::DeleteService( $ServiceConfig->{name} ) ) { print "The $ServiceConfig->{display} was successfully removed.\n"; } else { print "Failed to remove the $ServiceConfig->{display} service.\nEr +ror: " . GetError() . "\n"; } } sub keep_alive { my $Message = Win32::Daemon::QueryLastMessage( 1 ); Log($Message); }

2005-03-02 Janitored by Arunbear - added readmore tags, as per Monastery guidelines

janitored by ybiC: Remove extraneous trailing "HELP NEEDED!" from node title


In reply to Perl script as windows service by rockets12345

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.