Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Okay, summary of steps based on reading a handful of web pages and dissecting jryan's Perl script at the top of this thread.

This should be deployable almost as is. About the only things you should need to change are references to where the Perl binary is located and which directory into which you drop all files from this example. Global change and replace should be sufficient on these strings:

  • C:\\Perl\\bin\\perl.exe
  • C:\Steve\Dev\perlservice-perl
  • C:\\Steve\\Dev\\perlservice-perl

Without further adieu:

  1. I wrote a Perl script which does something to confirm that the service ran the Perl script.
     perlservicetest.pl :
    #!/usr/bin/perl my $statim = time; open LOGFIL, ">>perlservicetest.log"; print LOGFIL "$statim perlservicetest START\n"; close LOGFIL; exit; __END__
  2. I created a registry file to set the parameters needed to make the service run. This was based almost entirely on jryan's script at the start of this thread.
     perlservicetest.reg :
    Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\perlservicetest\ +Parameters] "Application"="C:\\Perl\\bin\\perl.exe" "AppDirectory"="C:\\Steve\\Dev\\perlservice-perl" "AppParameters"="C:\\Steve\\Dev\\perlservice-perl\\perlservicetest.pl"

    (Don't forget the extra blank line at the end; it is not optional.)
    (Also -- where shown, the doubled backslashes are not optional.)
     
  3. I copied the  instsrv.exe  and  srvany.exe  files to the same directory as the Perl script.
     
  4. I created a batch file to install and start up the new service (which also ensures any old versions are first stopped and removed).

     perlservicetest-install.bat :

    @echo off echo ----------------------------------------------------------------- +-------------- echo Checking logfile contents echo ----------------------------------------------------------------- +-------------- type perlservicetest.log pause echo ----------------------------------------------------------------- +-------------- echo Checking service status echo ----------------------------------------------------------------- +-------------- sc qc "perlservicetest" pause echo ----------------------------------------------------------------- +-------------- echo Stopping old service echo ----------------------------------------------------------------- +-------------- net stop perlservicetest pause echo ----------------------------------------------------------------- +-------------- echo Removing old service echo ----------------------------------------------------------------- +-------------- instsrv perlservicetest remove sc qc "perlservicetest" pause echo ----------------------------------------------------------------- +-------------- echo Installing new service echo ----------------------------------------------------------------- +-------------- instsrv perlservicetest C:\Steve\Dev\perlservice-perl\srvany.exe sc qc "perlservicetest" pause echo ----------------------------------------------------------------- +-------------- echo Modifying new service parameters echo ----------------------------------------------------------------- +-------------- REM start /WAIT "Registry Update" perlservicetest.reg regedit /s C:\Steve\Dev\perlservice-perl\perlservicetest.reg pause echo ----------------------------------------------------------------- +-------------- echo Starting new service echo ----------------------------------------------------------------- +-------------- net start perlservicetest pause echo ----------------------------------------------------------------- +-------------- echo Checking logfile contents echo ----------------------------------------------------------------- +-------------- type perlservicetest.log echo ----------------------------------------------------------------- +--------------

Then, to remove the service:

  1. I created a registry file to remove the service configuration information.
     perlservicetest-delete.reg :
    Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\perlservicetest\ +Parameters] "Application"=- "AppDirectory"=- "AppParameters"=- [-HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\perlservicetest +\Parameters] [-HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\perlservicetest +]

    (Again, remember -- the extra blank line at the end is not optional.)
     
  2. I created a batch file to stop and remove the service.
     perlservicetest-uninstall.bat :
    @echo off echo ----------------------------------------------------------------- +-------------- echo Checking logfile contents echo ----------------------------------------------------------------- +-------------- type perlservicetest.log pause echo ----------------------------------------------------------------- +-------------- echo Checking service status echo ----------------------------------------------------------------- +-------------- sc qc "perlservicetest" pause echo ----------------------------------------------------------------- +-------------- echo Stopping old service echo ----------------------------------------------------------------- +-------------- net stop perlservicetest pause echo ----------------------------------------------------------------- +-------------- echo Removing old service echo ----------------------------------------------------------------- +-------------- instsrv perlservicetest remove sc qc "perlservicetest" pause echo ----------------------------------------------------------------- +-------------- echo Removing service parameters echo ----------------------------------------------------------------- +-------------- regedit /s C:\Steve\Dev\perlservice-perl\perlservicetest-delete.reg echo ----------------------------------------------------------------- +--------------

Once you have one that works, you can adjust individual components to customize it to your needs. Maybe you want  instsrv.exe  and  srvany.exe  to live somewhere common. Maybe you want to change the service name from  perlservicetest . Maybe you want the Perl script to do more than write lines to a log file.

If the change you make doesn't give you the results you are looking for, roll back to the last one that worked, and keep chiseling away at it, back and forth, until you have molded it into your personal Windows Service masterpiece.

:-)


In reply to Re: Install a perl script as a Win NT/2000/XP service. by marinersk
in thread Install a perl script as a Win NT/2000/XP service. by jryan

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (8)
    As of 2014-12-18 23:55 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (69 votes), past polls