Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Perl: the Markov chain saw
 
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
[reply]

      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?

[reply]
        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
[reply]
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;
[reply]
[d/l]
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.
[reply]
[reply]

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
Community Ads
Chatterbox
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users
Others avoiding work at the Monastery: (6)
GrandFather
atcroft
herveus
Eyck
gnosti
im2
As of 2009-11-21 08:52 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

Future historians will find that the material characteristic of the current era is...

Aluminium
Plastic
Oil
Water
Carbon dioxide
Copper
Iron
Silicon
Salt
Uranium
Hydrogen
Other

Results (729 votes), past polls