Recipe 7.2. Running Any Program or Script as a ServiceProblemYou want to run a program or script as a service. This is useful when you want a program to run continuously regardless if someone is logged in. SolutionThe following solutions install the Perl script monitor.pl as a service named MyMonitor. Using a graphical user interface
Using a command-line interface:Run the following four commands to install the MyMonitor service: > instsrv MyMonitor "C:\Windows Resource Kits\Tools\srvany.exe" > reg add HKLM\System\CurrentControlSet\Services\MyMonitor\Parameters /v Application /d "c:\perl\bin\perl.exe" > reg add HKLM\System\CurrentControlSet\Services\MyMonitor\Parameters /v AppParameters /d "C:\scripts\monitor.pl" > sc start MyMonitor Using VBScript' This code creates and starts the MyMonitor Perl service ' ------ SCRIPT CONFIGURATION ------ strComputer = "." strSvcName = "MyMonitor" strSrvAnyPath = "c:\Windows Resource Kits\Tools\srvany.exe" strPerlPath = "c:\perl\bin\perl.exe" strPerlScript = "c:\scripts\monitor.pl" ' ------ END CONFIGURATION --------- const HKLM = &H80000002 ' Service Type Const KERNEL_DRIVER = 1 Const FS_DRIVER = 2 Const ADAPTER = 4 Const RECOGNIZER_DRIVER = 8 Const OWN_PROCESS = 16 Const SHARE_PROCESS = 32 Const INTERACTIVE_PROCESS = 256 INTERACT_WITH_DESKTOP = FALSE ' Error Control Const NOT_NOTIFIED = 0 Const USER_NOTIFIED = 1 Const SYSTEM_RESTARTED = 2 Const SYSTEM_STARTS = 3 set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") set objService = objWMI.Get("Win32_Service") intRC = objService.Create(strSvcName, _ strSvcName, _ strSrvAnyPath, _ OWN_PROCESS, _ NOT_NOTIFED, _ "Automatic", _ INTERACT_WITH_DESKTOP, _ "NT AUTHORITY\LocalService",_ "") if intRC > 0 then WScript.Echo "Error creating service: " & intRC WScript.Quit else WScript.Echo "Successfully created service" end if strKeyPath = "SYSTEM\CurrentControlSet\Services\" & _ strSvcName & "\Parameters" set objReg = GetObject("winmgmts:\\" & _ strComputer & "\root\default:StdRegProv") objReg.CreateKey HKLM,strKeyPath objReg.SetStringValue HKLM,strKeyPath,"Application",strPerlPath objReg.SetStringValue HKLM,strKeyPath,"AppParameters",strPerlScript WScript.Echo "Created registry values" set objService = objWMI.Get("Win32_Service.Name='" & strSvcName & "'") intRC = objService.StartService if intRC > 0 then WScript.Echo "Error starting service: " & intRC else WScript.Echo "Successfully started service" end if DiscussionDo you have cool script or an executable that you'd like to run continuously? You could use Task Scheduler to periodically run the job, but that doesn't give you much flexibility to stop, start, and monitor the job. Another option is to turn the program into a service as outlined in the solutions. By doing this, the program will run continuously and no one needs to be logged in. To run a script or executable as a service, you need the help of another program called srvany.exe from the Resource Kit. It acts as a wrapper around your script or executable by handling all the service control messages (e.g., stop, start, pause, etc.). Creating a service consists of setting a few registry keys and values under the HKLM\SYSTEM\CurrentControlSet\Services\ key. For a SrvAny service, you need to configure several values under the new service's Parameter key. The Application value should contain the path to the script or executable you want to run. In this case, since I'm using Perl, I set this to the Perl executable. And since I need to pass the name of the Perl script to the executable, I created an AppParameters key, which contains any parameters (in this case, the script path) to the executable (specified in Application).
See AlsoMS KB 137890 (HOWTO: Create a User-Defined Service), MS KB 821794 (INFO: Best Practices When You Create Windows Services), and MSDN: Create Method of the Win32_Service Class |