Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Trying to start a perl script as Windows Service

Reply
Thread Tools

Trying to start a perl script as Windows Service

 
 
dn.perl@gmail.com
Guest
Posts: n/a
 
      09-28-2007

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?

 
Reply With Quote
 
 
 
 
Bill H
Guest
Posts: n/a
 
      09-28-2007
On Sep 28, 12:45 pm, "(E-Mail Removed)" <(E-Mail Removed)> wrote:
> 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

 
Reply With Quote
 
 
 
 
dn.perl@gmail.com
Guest
Posts: n/a
 
      09-28-2007
On Sep 28, 11:46 am, Bill H <(E-Mail Removed)> wrote:
> On Sep 28, 12:45 pm, "(E-Mail Removed)" <(E-Mail Removed)> wrote:
>
>
>
>
>
> > 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?)
>
>


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.

--------------


 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      09-29-2007

Quoth "(E-Mail Removed)" <(E-Mail Removed)>:
> On Sep 28, 11:46 am, Bill H <(E-Mail Removed)> wrote:
> > On Sep 28, 12:45 pm, "(E-Mail Removed)" <(E-Mail Removed)> wrote:
> >
> > > 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.

> >
> > 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.


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

 
Reply With Quote
 
dn.perl@gmail.com
Guest
Posts: n/a
 
      10-03-2007
On Sep 28, 5:30 pm, Ben Morrow <(E-Mail Removed)> wrote:
>
> 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.

 
Reply With Quote
 
dn.perl@gmail.com
Guest
Posts: n/a
 
      10-03-2007

On Sep 28, 9:45 am, "(E-Mail Removed)" <(E-Mail Removed)> wrote:
>
> 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.


 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      10-03-2007

Quoth "(E-Mail Removed)" <(E-Mail Removed)>:
> On Sep 28, 5:30 pm, Ben Morrow <(E-Mail Removed)> wrote:
> >
> > 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.


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

 
Reply With Quote
 
Ron Bergin
Guest
Posts: n/a
 
      10-04-2007
On Oct 3, 11:14 am, "(E-Mail Removed)" <(E-Mail Removed)> wrote:
> On Sep 28, 5:30 pm, Ben Morrow <(E-Mail Removed)> wrote:
>
>
>
> > 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.


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

Win32:aemon::Simple
http://search.cpan.org/~jenda/Win32-....2.6/Simple.pm

 
Reply With Quote
 
dn.perl@gmail.com
Guest
Posts: n/a
 
      10-19-2007
On Oct 3, 8:59 pm, Ron Bergin <(E-Mail Removed)> wrote:
>
> I've never used it myself, but you could try Win32:aemonhttp://search.cpan.org/~daveroth/Win32-Scheduler_v20000702/lib/Win32/...http://www.roth.net/perl/Daemon/
>
> Win32:aemon::Simplehttp://search.cpan.org/~jenda/Win32-Daemon-Simple-0.2.6/Simple.pm


I tried : http://search.cpan.org/~daveroth/Win...2/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:aemon::Simple.

I installed a service "n_print_tstamp" with the script
"my_install_service.pl" :
use Win32:aemon;
%Hash = (
name => 'n_print_tstamp',
display => 'print_tstamp',
path => 'c:\perl\bin\perl.exe',
user => '',
pwd => '',
parameters =>'C:\mydir\print_tstamp.pl',
);
if( Win32:aemon::CreateService( \%Hash ) )
{
print "Successfully added.\n";
}
else
{
print "Failed to add service: " .
Win32::FormatMessage( Win32:aemon::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 :
-----------------
Could not start the print_stamp service on local computer.
Error 1069 : the service did not start due to a logon failure.
--------------

What might be the issue?


 
Reply With Quote
 
wbosse@metzler.com
Guest
Posts: n/a
 
      10-19-2007
On 19 Okt., 05:32, "(E-Mail Removed)" <(E-Mail Removed)> wrote:

[...]

> 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:aemon 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 :
> -----------------
> Could not start the print_stamp service on local computer.
> Error 1069 : the service did not start due to a logon failure.
> --------------
>
> What might be the issue?


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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can perl start another perl script and then exit? Bill H Perl Misc 3 09-18-2007 12:07 AM
Execute another perl script from my perl script Petterson Mikael Perl Misc 3 01-05-2005 01:31 PM
problem calling perl script from SOAP server perl script pj Perl Misc 3 04-09-2004 10:23 PM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM
How to make Perl Script "POST" call from another Perl Script??? Wet Basement Perl 1 07-15-2003 10:25 PM



Advertisments