Trying to start a perl script as Windows Service

D

dn.perl

I want to run a perl script which will continue running on Windows XP
even after I log off, the way nohup works in Unix. A recommended way
of doing it is to run the application as Windows Service. Accordingly
I wrote a test script (myscript.pl), which prints a line every 10
seconds in an infinite loop :
Time is 2007-09-27 16:22:25
Time is 2007-09-27 16:22:35
Time is 2007-09-27 16:22:45

I created a service for the perl script. When I start the service, it
stops immediately.
The message is : The print_tstamp service on local computer started
and then stopped. Some services stop automatically if they have no
work to do, for example, the Performance Logs and Alerts service.

Any suggestions, please?
 
B

Bill H

I want to run a perl script which will continue running on Windows XP
even after I log off, the way nohup works in Unix. A recommended way
of doing it is to run the application as Windows Service. Accordingly
I wrote a test script (myscript.pl), which prints a line every 10
seconds in an infinite loop :
Time is 2007-09-27 16:22:25
Time is 2007-09-27 16:22:35
Time is 2007-09-27 16:22:45

I created a service for the perl script. When I start the service, it
stops immediately.
The message is : The print_tstamp service on local computer started
and then stopped. Some services stop automatically if they have no
work to do, for example, the Performance Logs and Alerts service.

Any suggestions, please?

Just a guess - but did it stop because you closed the window, which
means it had no place to print (maybe gets an error?)

Bill H
 
D

dn.perl

Just a guess - but did it stop because you closed the window, which
means it had no place to print (maybe gets an error?)

Well, I am printing (concatenating) the string to a file.
I tried to run the service with perl, and also with wperl.
But so far nothing has worked. When I log off, the process
terminates.

--------------
 
B

Ben Morrow

Quoth "[email protected] said:
Well, I am printing (concatenating) the string to a file.
I tried to run the service with perl, and also with wperl.
But so far nothing has worked. When I log off, the process
terminates.

To run an arbitrary program as a windows service you need to use srvany
from the Resource Kit. I don't really know why, but it's to do with how
Windows expects a service to behave. See
http://support.microsoft.com/kb/137890 .

Ben
 
D

dn.perl

To run an arbitrary program as a windows service you need to use srvany
from the Resource Kit. I don't really know why, but it's to do with how
Windows expects a service to behave. Seehttp://support.microsoft.com/kb/137890.

I have used srvany from the Resource Kit and followed the steps from
the above URL. But the service stops immediately after I start it. It
is supposed to print one line every 20 seconds to a file in an
infinite loop. It is not supposed to print anything to stdout, so it
should really run in the background and the question of it having 'no
place' to print (as suggested by Bill H) does not exist. Some services
stop after starting because 'they have nothing to do'. But this
process does have something to do and yet it stops.
 
D

dn.perl

I created a service for the perl script. When I start the service, it
stops immediately.

This could be more a Windows problem that a perl one.

Having failed to start a perl script as a service on Windows XP, I
tried to start it via scheduler. The script

prints a line every 10 seconds to a file and runs continuously. I
tried to run it under perl and also wperl. The

script should run even when I am logged off, but I failed to manage it
both with perl and wperl.

Case 1 : If I am logged in when the scheduler starts the script, the
script terminates when I log off.
Case 2 : If I am not logged in when the scheduler starts it, the
script continues when I log off.
I don't want it to terminate in case 1 either.

If the script is started under perl (as against wperl), whether by the
scheduler or manually, and I try to log off,

I get the standard error message: Windows cannot end this program, etc
etc.
Two choices are offered : End Now or Cancel. If I cancel, and try
logging off again, this time Windows does end the program, and logs me
off. The script is terminated.

I want to achieve an effect similar to 'nohup' wherein the program
keeps running after the session which initiated it is terminated. But
'srvany.exe' from Windows Resource Kit isn't making it possible and
running it as Scheduled Process (or running it as service) is also not
working.
 
B

Ben Morrow

Quoth "[email protected] said:
I have used srvany from the Resource Kit and followed the steps from
the above URL. But the service stops immediately after I start it. It
is supposed to print one line every 20 seconds to a file in an
infinite loop. It is not supposed to print anything to stdout, so it
should really run in the background and the question of it having 'no
place' to print (as suggested by Bill H) does not exist. Some services
stop after starting because 'they have nothing to do'. But this
process does have something to do and yet it stops.

Can you try reopening STDERR to a file somewhere (probably best if you
open it in append mode) *really* early on in your script? Something like

BEGIN {
# no point giving a sensible message as there's nowhere for it
# to go :(
open STDERR, '>>', 'c:/path/to/log.file' or die;
}

right at the top. Then you've got more of a chance of finding out what's
wrong. I suspect it's some sort of environment issue: perhaps the local
SYSTEM user doesn't have some permission it requires?

Ben
 
R

Ron Bergin

I have used srvany from the Resource Kit and followed the steps from
the above URL. But the service stops immediately after I start it. It
is supposed to print one line every 20 seconds to a file in an
infinite loop. It is not supposed to print anything to stdout, so it
should really run in the background and the question of it having 'no
place' to print (as suggested by Bill H) does not exist. Some services
stop after starting because 'they have nothing to do'. But this
process does have something to do and yet it stops.

I've never used it myself, but you could try Win32::Daemon
http://search.cpan.org/~daveroth/Win32-Scheduler_v20000702/lib/Win32/Scheduler.PM
http://www.roth.net/perl/Daemon/

Win32::Daemon::Simple
http://search.cpan.org/~jenda/Win32-Daemon-Simple-0.2.6/Simple.pm
 
D

dn.perl

I've never used it myself, but you could try Win32::Daemonhttp://search.cpan.org/~daveroth/Win32-Scheduler_v20000702/lib/Win32/...http://www.roth.net/perl/Daemon/

Win32::Daemon::Simplehttp://search.cpan.org/~jenda/Win32-Daemon-Simple-0.2.6/Simple.pm

I tried : http://search.cpan.org/~daveroth/Win32-Scheduler_v20000702/lib/Win32/Scheduler.PM
and ran into roughly the same problem (permissions related) which I
had faced when using SRVANY.exe file. I still haven't tried out the
steps detailed in Win32::Daemon::Simple.

I installed a service "n_print_tstamp" with the script
"my_install_service.pl" :
use Win32::Daemon;
%Hash = (
name => 'n_print_tstamp',
display => 'print_tstamp',
path => 'c:\perl\bin\perl.exe',
user => '',
pwd => '',
parameters =>'C:\mydir\print_tstamp.pl',
);
if( Win32::Daemon::CreateService( \%Hash ) )
{
print "Successfully added.\n";
}
else
{
print "Failed to add service: " .
Win32::FormatMessage( Win32::Daemon::GetLastError() ) . "\n";
}


The service was successfully added. I have verified that C:\mydir
\print_tstamp.pl runs correctly from the command prompt.
Listing of print_tstamp.pl (which is an infinite loop) :
#! sample perl str -w

use strict ;
use POSIX qw(strftime);

$0 =~ m/(.*)\\.*/ ;
my $exec_dir = $1 ;

my $aa ;
my $str ;
my $filename = "$exec_dir\\tstamp_log.wri" ;

my $sleep_time = 5 ;

for ($aa = 1; $aa < 3; $aa++) {
sleep $sleep_time if $aa != 1 ;
open (FH, ">>", $filename) or die "cannot open the file\n" ;
$str = strftime "%Y-%m-%d %H:%M:%S ", localtime;
# print "Time is $str \n" ;
print FH "Time is $str \n" ;
close FH ;
$aa = 1 ;
}

When I try to start the above script as service, I get some error or
other.

Error message for blank user and blank pwd in %hash in
my_install_service.pl.
--------------
Could not start the print_stamp service on local computer.
Error 1053 : The service did not respond to the start or control
request in a timely fashion
--------------



Error message for my_install_service.pl file with invalid username-
password :
-----------------
Failed to add service: The account name is invalid or does not exist,
or the password is invalid for the account name specified.
==> This error message shows that the service is being started only
after verifying username-password.
--------------


Error message for my_install_service.pl file with proper username-
password
in the hash declaration in my_install_service.pl :
 
W

wbosse

[...]
Error message for blank user and blank pwd in %hash in
my_install_service.pl.
--------------
Could not start the print_stamp service on local computer.
Error 1053 : The service did not respond to the start or control
request in a timely fashion
--------------

The Win32::Daemon dokumentation contains code skeletons demonstrating
how to deal with service control messages/states.
Error message for my_install_service.pl file with proper username-
password
in the hash declaration in my_install_service.pl :

In "my_install_service.pl" change "pwd" to "password". This is an
error in the Win32::Daemon manual.
 
D

dn.perl

[...]
Error message for blank user and blank pwd in %hash in
my_install_service.pl.
--------------
Could not start the print_stamp service on local computer.
Error 1053 : The service did not respond to the start or control
request in a timely fashion
--------------

The Win32::Daemon dokumentation contains code skeletons demonstrating
how to deal with service control messages/states.
Error message for my_install_service.pl file with proper username-
password
in the hash declaration in my_install_service.pl :
What might be the issue?

In "my_install_service.pl" change "pwd" to "password". This is an
error in the Win32::Daemon manual.

If I set : pwd => 'correct_passwd' , I get the error about 'logon
failure'.
I changed it to : password => 'correct_passwd'

But now only the error has changed. Like for blank user-passwd combo,
even for valid-password combo, I get the error about 'timely fashion'.
Error message when I try to start the service from Start > Control
Panel > Admin Tools > Services > Choose Service + Right Click + Start.
"Error 1053 : The service did not respond to the start or control
request in a timely fashion"

I have not yet tried to start it from a perl script, but will check
whether there is any way of doing so. Any suggestion is welcome in the
meantime.
 
W

wbosse

If I set : pwd => 'correct_passwd' , I get the error about 'logon
failure'.
I changed it to : password => 'correct_passwd'

But now only the error has changed. Like for blank user-passwd combo,
even for valid-password combo, I get the error about 'timely fashion'.

However the logon problem is solved.
Error message when I try to start the service from Start > Control
Panel > Admin Tools > Services > Choose Service + Right Click + Start.
"Error 1053 : The service did not respond to the start or control
request in a timely fashion"

This error is normal as long as your script does not respond to
service control messages resp. sets the service state. Again: See the
service templates in the Win32::Daemon manual page.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,737
Latest member
Georgeengab

Latest Threads

Top