Recipe7.2.Running Any Program or Script as a Service


Recipe 7.2. Running Any Program or Script as a Service

Problem

You 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.

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

    Right-click on MyMonitor and select New

    Enter Parameters and press Enter.

  12. Right-click on Parameters and select New

    Enter Application and press Enter twice.

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

  14. Right-click on Parameters and select New

    Enter AppParameters and press Enter twice.

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

  16. Open the Services snap-in.

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

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

Discussion

Do 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).

Not all executables work well when running in the context of a service. Some programs may stop working when a logoff event occurs and others may not work at all. Your mileage will vary, so test your custom service thoroughly.


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 Server Cookbook
Windows Server Cookbook for Windows Server 2003 and Windows 2000
ISBN: 0596006330
EAN: 2147483647
Year: 2006
Pages: 380
Authors: Robbie Allen

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