Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
Just another Perl shrine
 
PerlMonks

Install a perl script as a Win NT/2000/XP service.

by jryan (Vicar)
 | Log in | Create a new user | The Monastery Gates | Super Search | 
 | Seekers of Perl Wisdom | Meditations | PerlMonks Discussion | 
 | Obfuscation | Reviews | Cool Uses For Perl | Perl News | Q&A | Tutorials | 
 | Poetry | Recent Threads | Newest Nodes | Donate | What's New | 

on Jan 27, 2003 at 22:23 UTC ( #230377=snippet: print w/ replies, xml ) Need Help??

Description: This perl script uses srvany and instsrv (two free programs from Microsoft; put on my perlmonk.org website for ease of linking) to install a perl script as a Windows NT/2000/XP service. Pretty handy, if you ask me.
#!perl -w
use strict;
use FindBin;
use Config;

while(1) {
    print "Please enter your script's name:\n";
    my $srvname = <>;
    chomp $srvname;
    my($srv) = $srvname =~ /^(\w+)/;

    if ($srv =~ /[a-z]\w+/i) {
        my $dir = $FindBin::Bin;
        $dir =~ s/\//\\\\/g;
        my $ret = `instsrv $srv $dir\\srvany.exe`;
        if ($ret =~ /success/i) {
            my $perlpath = $Config{perlpath};
            $perlpath =~ s/\\/\\\\/g;
            my $srvpath = $FindBin::Bin;
            $srvpath =~ s/(?<=\\)[^\\]+$//;
            $srvpath =~ s/\//\\\\/g;
            $srvpath .= "\\\\$srvname";

            my $reg = qq(
            REGEDIT4

                [HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Servic
+es\\)
                .$srvname.qq(\\Parameters]
                "Application"="$perlpath"
                "AppParameters"="$srvpath"
            );
            $reg =~ s/^\n//;
            my ($space) = $reg =~ /^(\s+)/;
            $reg =~ s/^$space//gm;

            open (OUT, ">reg.reg") or die $!;
            print OUT $reg."\n";
            close OUT;

            my $errormsg = qq(
                              Couldn't automatically add necessary inf
+ormation
                              to registry... generating necessary regi
+stry
                              file, please add manually by double-clic
+king on
                              it.
                             );
            $errormsg =~ s/(?<=\n)\s+//g;
            system ("reg.reg") and die $errormsg;
            unlink "reg.reg";
            print "\n\n$ret";
            exit 0;
        }
        else {
            die qq(Error installing $srvname as service: "$ret"\n);
        }
    }
    else {
        my $errormsg =  qq(
                           Error: Your script's name must start with a
+ letter,
                           and can only contain letters, numbers, and 
+the
                           underscore.  Please try again.
                          );
           $errormsg =~ s/(?<=\n)\s+//g;
        print $errormsg;
    }
}
Comment on Install a perl script as a Win NT/2000/XP service.
Download Code
Re: Install a perl script as a Win NT/2000/XP service.
by Aristotle (Chancellor) on Jan 28, 2003 at 01:45 UTC

      Could you care to elaborate? I see nothing in the node you point to that says "broken", only "bizarre." I've been using FindBin successfully for awhile now; can you give an example where it would break, especially in a script like this?

        I tried to use Win32::Daemon. I am using XP professional. Although I could install my perl script as a service, when I restarted my XP, the service never started. I checked the log in event viewer and it says "Timeout (30000 milliseconds) waiting for the Directory Monitoring Service service to connect." I tried running the example dirmon script from Dave Roth's site and it behaved in same way. My questions are 1) has dirmon script from dave roth's site worked for anybody on XP Pro. 2) My XP does not have XP service pack 1 on it. Do I need the service pack installed for this to work? 3) Or have I completely missed something. Thanks Jeevan Savant
Re: Install a perl script as a Win NT/2000/XP service.
by ibanix (Hermit) on Jan 28, 2003 at 03:10 UTC
    Very interesting.

    I prefer to compile my scripts with perl2exe and install the binary as a service. I've used instsrv and srvany, but I prefer to use the freely available FireDaemon. Get the v0.09 version -- the new version only supports 1 service without a registration code.

    You might also consider Win32::Daemon for a pure-perl solution.

    Update: Srvany.exe and instsrv.exe are part of the Windows NT 4.0 Resource Kit, available here.

    Cheers,
    ibanix

    $ echo '$0 & $0 &' > foo; chmod a+x foo; foo;
Re: Install a perl script as a Win NT/2000/XP service.
by AcidHawk (Vicar) on Jan 28, 2003 at 13:57 UTC

    Have you looked at the Win32::Daemon module.
    This can be compiled and installed without using any sryany.exe type service stubs..

    Example can be found here.

    -----
    Of all the things I've lost in my life, its my mind I miss the most.

Back to Snippets Section

Login:
Password
remember me
What's my password?
Create A New User

Node Status?
node history
Node Type: snippet [id://230377]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (25)
GrandFather
ysth
ww
jdporter
toolic
holli
Gavin
kennethk
MidLifeXis
thezip
Eyck
pileofrogs
erix
socketdave
SuicideJunkie
hominid
redlemon
meraxes
ssandv
VinsWorldcom
thedoe
MikeDexter
cmg
brp4h
im2
As of 2010-02-09 20:57 GMT
Sections?
The Monastery Gates
Seekers of Perl Wisdom
Meditations
PerlMonks Discussion
Categorized Q&A
Tutorials
Obfuscated Code
Perl Poetry
Cool Uses for Perl
Perl News
Information?
PerlMonks FAQ
Guide to the Monastery
What's New at PerlMonks
Voting/Experience System
Tutorials
Reviews
Library
Perl FAQs
Other Info Sources
Find Nodes?
Nodes You Wrote
Super Search
List Nodes By Users
Newest Nodes
Recently Active Threads
Selected Best Nodes
Best Nodes
Worst Nodes
Saints in our Book
Leftovers?
The St. Larry Wall Shrine
Offering Plate
Awards
Craft
Snippets Section
Code Catacombs
Quests
Editor Requests
Buy PerlMonks Gear
PerlMonks Merchandise
Planet Perl
Perlsphere
Use Perl
Perl.com
Perl 5 Wiki
Perl Jobs
Perl Mongers
Perl Directory
Perl documentation
CPAN
Random Node
Voting Booth?

What level of existential comfort do you require?

Palace
Executive suite at the best hotel
Regular hotel in a decent part of town
Motel
Boarding house
Sleeping Bag on Couch in Basement
Any port in a storm
Camping under the freeway overpass
Jail
Other

Results (279 votes), past polls