Recipe 10.17. Running Any Program or Script as a Service


Problem

You want to run a program or script as a service. This is useful whether you want a program to run continuously regardless of whether someone is logged in.

Solution

The following solutions install the Perl script monitor.pl as a service named MyMonitor.

Using a graphical user interface

  1. Open the Service Creation Wizard (srvinstw.exe).

  2. Select Install a service and click Next.

  3. Select the target machine to install the service on and click Next.

  4. Enter MyMonitor for the service name and click Next.

  5. Enter the path of the srvany.exe executable and click Next.

  6. Select Service is its own process and click Next.

  7. Select the account to run the service under and click Next.

  8. Select the service startup type and click Next.

  9. Click Finish.

  10. Open the Registry Editor (regedit.exe).

  11. In the left pane, browse to the service's Registry key by opening HKEY_LOCAL_MACHINE SYSTEM CurrentControlSet Services MyMonitor.

  12. Right-click on MyMonitor and select New Key.

  13. Right-click on Parameters and select New String Value.

  14. Enter the path to the Perl executable (e.g., c:\perl\bin\perl.exe) and click OK.

  15. Right click on Parameters and select New String Value.

  16. Enter the path to the Perl script (e.g., c:\scripts\monitor.pl) and click OK.

  17. Open the Services snap-in.

  18. In the left pane, right-click on MyMonitor and select Start.

Using a command-line interface
> 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

Discussion

Do you have a cool script or executable that you'd like to run continuously? You could use Task Scheduler to periodically run the job, but that doesn't give you a lot of flexibility over stopping, starting, or monitoring the job. Another option is to turn the program into a service as we outlined in the solutions. By doing this, the program will run continuously and no one needs to be logged in for it to run.

To turn a script or executable into a service, you need the help of another program called srvany.exe from the Resource Kit. SrvAny acts as a wrapper around your script or executable by handling all the service control messages (e.g., stop, start, pause).

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 we're using Perl, we set the Application value to the Perl executable. And since we need to pass the name of the Perl script to the executable, we create an AppParameters key, which contains any parameters (in this case the script path) to the executable.

See Also

MS 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



Windows XP Cookbook
Windows XP Cookbook (Cookbooks)
ISBN: 0596007256
EAN: 2147483647
Year: 2006
Pages: 408

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net