Windows – Run a perl script as a windows 7 service

perlwindows

I have a perl script which is compiled using pp, to be run as a windows service on windows 7 machines. I looked at the thread
http://www.perlmonks.org/index.pl?node%5Fid=230377 but of little use because most of them weren't clear and the solutions suggested in that were to create executables and not actually for running as a windows 7 service.
I tried putting my compiled exe in the scheduled tasks of windows but I think its not able to run for some reason. How do I debug this?

Best Answer

You can create a service in Windows 7 by using the sc.exe command from the command line as an administrator. Here's how you do it.

Go to Start -> All Programs -> Accessories and then right-click on "Command Prompt". From the pop-up menu choose "Run as administrator".

Once in the command prompt use the following command to create a service. Note that there is a space after binPath= and it is required.

sc create perlsvc binPath= c:\myprogram\myperlprg.exe

This will create a service named perlsvc that executes c:\myprogram\myperlprg.exe when the service is started. If you now run services.msc you should see your service listed. Your service will be set to start manually at this point. You can change it from within the services program you just started or via an option when you create the service (see the link at the end of this post).

The value perlsvc is used as the registry key for the service1 and also to refer to the service with the sc and net commands. It's best to keep it short and without spaces. If you'd like something more descriptive, you can append the DisplayName= option. Again, take note of the mandatory space after the equal sign.

sc create perlsvc binPath= c:\myprogram\myperlprg.exe DisplayName= "Important Perl Service"

When you run services.msc you will see "Important Perl Service" as the service name. This name will also be shown when you run net start to see the list of running services from the CLI. You can still use the short name when in the CLI, such as with net start perlsvc.

If you no longer need your service, run sc delete perlsvc and it'll be removed.

If you plan on scripting the installation of the service, you may be interested in reading about the other options for sc documented at Microsoft's web site.


1 Services are located in the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services registry key.