Trying to start a perl script as Windows Service

Discussion in 'Perl Misc' started by dn.perl@gmail.com, Sep 28, 2007.

  1. Guest

    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?
    , Sep 28, 2007
    #1
    1. Advertising

  2. Bill H Guest

    On Sep 28, 12:45 pm, "" <> 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
    Bill H, Sep 28, 2007
    #2
    1. Advertising

  3. Guest

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

    --------------
    , Sep 28, 2007
    #3
  4. Ben Morrow Guest

    Quoth "" <>:
    > On Sep 28, 11:46 am, Bill H <> wrote:
    > > On Sep 28, 12:45 pm, "" <> 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
    Ben Morrow, Sep 29, 2007
    #4
  5. Guest

    On Sep 28, 5:30 pm, Ben Morrow <> 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.
    , Oct 3, 2007
    #5
  6. Guest

    On Sep 28, 9:45 am, "" <> 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.
    , Oct 3, 2007
    #6
  7. Ben Morrow Guest

    Quoth "" <>:
    > On Sep 28, 5:30 pm, Ben Morrow <> 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
    Ben Morrow, Oct 3, 2007
    #7
  8. Ron Bergin Guest

    On Oct 3, 11:14 am, "" <> wrote:
    > On Sep 28, 5:30 pm, Ben Morrow <> 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::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
    Ron Bergin, Oct 4, 2007
    #8
  9. Guest

    On Oct 3, 8:59 pm, Ron Bergin <> wrote:
    >
    > 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 :
    -----------------
    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?
    , Oct 19, 2007
    #9
  10. Guest

    On 19 Okt., 05:32, "" <> 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::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 :
    > -----------------
    > 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::Daemon manual.
    , Oct 19, 2007
    #10
  11. Guest

    On Oct 19, 9:10 am, wrote:
    > On 19 Okt., 05:32, "" <> 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::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 :
    > > -----------------
    > > 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::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.
    , Oct 19, 2007
    #11
  12. Guest

    On 19 Okt., 19:24, "" <> wrote:
    > On Oct 19, 9:10 am, wrote:
    > > 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'.


    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.
    , Oct 22, 2007
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,767
  2. =?Utf-8?B?M2Ry?=
    Replies:
    2
    Views:
    10,913
    Steven Cheng[MSFT]
    Sep 7, 2004
  3. Anup
    Replies:
    1
    Views:
    2,763
    Mark Rae
    May 9, 2006
  4. Kai Geng
    Replies:
    0
    Views:
    180
    Kai Geng
    Dec 12, 2007
  5. Bill H
    Replies:
    3
    Views:
    129
    Bill H
    Sep 18, 2007
Loading...

Share This Page