Coral: cron srvstart utility: Difference between revisions
(Created page with "<div id="top" style="margin:0;padding: 0px 0px 0px 5px; color: #000; background-color: #FFFFEB; border: 1px solid darkslateblue;"><span id="top" style="margin:0;padding: 0px;font...") |
(Punctuation and grammatical changes.) |
||
Line 6: | Line 6: | ||
<span style="margin-bottom:5px;font-size:25px;color: #31799F;">CRON - SrvStart utility tutorial</span> | <span style="margin-bottom:5px;font-size:25px;color: #31799F;">CRON - SrvStart utility tutorial</span> | ||
This short tutorial covers the SrvStart utility written by Nick Rozanski. It allows you to run a script as a service! | This short tutorial covers using the SrvStart utility written by Nick Rozanski. It allows you to run a script as a service! | ||
'''What is a Service?'''<br /> | '''What is a Service?'''<br /> | ||
A Windows Service is automatically started when your PC starts. Services automatically recover from program crashes and Standby or | A Windows Service is automatically started when your PC starts. Services automatically recover from program crashes and Standby or Hibernation modes. Installing software as a service requires Administrator permissions. | ||
'''Complexity'''<br /> | '''Complexity'''<br /> | ||
An application program designed to run as a service contains code that interfaces to the Windows service manager. Running a script as a service requires additional code that allows it to access the Windows service manager. This code registers your script, allowing it to be run as a service. In addition it must provide responses to command requests from the service manager and have the ability to provide your script's status. | |||
It's not a trivial exercise, but this complexity is hidden when using SrvStart. | |||
Line 70: | Line 70: | ||
* svc.exe | * svc.exe | ||
We are | We are only interested in files '''srvstart.exe''', '''srvstart.dll''' and '''logger.dll'''<br /> | ||
For this tutorial create a new folder c:\'''service_test''' and copy the above three files into it. | For this tutorial, create a new folder c:\'''service_test''' and copy the above three files into it. | ||
'''Note 1:'''<br /> | '''Note 1:'''<br /> | ||
The documentation | The documentation says to copy files srvstart.exe, srvstart.dll, logger.dll and msvcrt.dll to C:\WINDOWS\system32 (system path). For XP onwards msvcrt.dll is already installed on a system path. The other three files do not need to be installed on a system path; see below. | ||
We are going to run srvstart.exe from its installation folder. This allows it to be portable by not installing anything on a host PC (assumes running from memory stick). | We are going to run srvstart.exe from its installation folder. This allows it to be portable by not installing anything on a host PC (assumes running from memory stick). | ||
'''Note 2:'''<br /> | '''Note 2:'''<br /> | ||
Notwithstanding the above, when installing a service, it does write to the registry, so you must remember to stop and uninstall the service you create if it resides on an external drive or a memory stick. | |||
==Script to run== | ==Script to run== | ||
For this tutorial we require a script to run. Create a new text document named '''my_script.vbs''' with the following content: | For this tutorial we require a script to run. Create a new text document named '''my_script.vbs''' with the following content: | ||
Line 97: | Line 95: | ||
</pre> | </pre> | ||
| | | | ||
This is an annoying VBScript | This is an annoying VBScript which just produces beeps when it runs. | ||
Double click on my_script.vbs and confirm it produces two beeps. | Double click on my_script.vbs and confirm that it produces two beeps. | ||
|} | |} | ||
===Limitation=== | ===Limitation=== | ||
An application to be run as a service must have one of the following file extensions .exe, .com or .bat The script we want to run has an extension .vbs | An application to be run as a service must have one of the following file extensions: .exe, .com or .bat. The script we want to run has an extension of .vbs so it cannot be run directly. | ||
At first this looks very restrictive however a batch file will run any application. | At first this looks very restrictive, however a batch file will run any application. | ||
To run our script as a service create a batch file named my_script.bat with the following content: | To run our script as a service, create a batch file named my_script.bat with the following content: | ||
{| | {| | ||
Line 117: | Line 113: | ||
</pre> | </pre> | ||
| | | | ||
'''start''' Runs our vb script and closes. It does not wait for our vb script to finish<br /> | '''start''' Runs our vb script and closes. It does not wait for our vb script to finish.<br /> | ||
Double click on my_script.bat and confirm it produces two beeps. | Double click on my_script.bat and confirm it produces two beeps. | ||
|} | |} | ||
Line 123: | Line 119: | ||
'''Note 1:''' Running my_script.bat a command window opens for a short time. When run as a service this is hidden.<br /> | '''Note 1:''' Running my_script.bat a command window opens for a short time. When run as a service this is hidden.<br /> | ||
'''Note 2:''' Removing the "start" keyword our vb script runs and the command window remains open until vb script finishes. Again when run as a service this is hidden. | '''Note 2:''' Removing the "start" keyword, our vb script runs and the command window remains open until vb script finishes. Again, when run as a service, this is hidden. | ||
==SrvStart configuration file== | ==SrvStart configuration file== | ||
To create our application (script) service SrvStart requires a configuration file. | To create our application (script) service SrvStart requires a configuration file. | ||
Line 150: | Line 145: | ||
|} | |} | ||
==Control_batch_files== | ==Control_batch_files== | ||
We are now ready to install and run our service | We are now ready to install and run our service. This can be done using a command prompt and issuing the appropriate commands. | ||
I prefer to put these commands into separate batch files as follows: | I prefer to put these commands into separate batch files as follows: | ||
Line 216: | Line 210: | ||
|} | |} | ||
==Test - Perceived problems== | ==Test - Perceived problems== | ||
{| | {| | ||
Line 228: | Line 221: | ||
Run '''z_service_start.bat''' | Run '''z_service_start.bat''' | ||
| | | | ||
Confirm two | Confirm two beeps are produced. Note the following messages: | ||
* The z_test service is starting. | * The z_test service is starting. | ||
* The z_test service could not be started. | * The z_test service could not be started. | ||
* The service did not report an error. | * The service did not report an error. | ||
* More help is available by typing NET HELPMSG 3534. | * More help is available by typing NET HELPMSG 3534. | ||
With the confirmation of two beeps our script was successfully run. | With the confirmation of two beeps, our script was successfully run. The reason for '''could not be started message'''; the batch file closes immediately when run. Srvstart regularly checks the running status of an application it started, it immediately sees the script is no longer running and reports back to the service control that it could not be started. | ||
The service did not report an error because it’s not an error. | The service did not report an error because it’s not an error. | ||
|-valign="top" | |-valign="top" | ||
Line 243: | Line 236: | ||
* The z_test service is not started. | * The z_test service is not started. | ||
* More help is available by typing NET HELPMSG 3521. | * More help is available by typing NET HELPMSG 3521. | ||
The batch file is not running hence the service as | The batch file is not running, hence the service is reported as not running; see above. | ||
The above is not a problem; it's the way we want to run our script. | |||
|-valign="top" | |-valign="top" | ||
| | | | ||
Line 253: | Line 246: | ||
Confirms correct operation | Confirms correct operation | ||
|} | |} | ||
'''Note:''' The above results may be a little confusing | '''Note:''' The above results may be a little confusing. Rremember our VBScript does not run continuously; it's a run-once program. Its purpose is to familiarise yourself with the SrvStart program. | ||
==Service Status== | ==Service Status== | ||
With the service created, it will now appear in the Windows Services list and can be configured like any other Windows Service. | With the service created, it will now appear in the Windows Services list and can be configured like any other Windows Service. | ||
Line 263: | Line 255: | ||
* It is set to Manual startup. | * It is set to Manual startup. | ||
* It starts using LocalSystem and has no dependencies. | * It starts using LocalSystem and has no dependencies. | ||
* | * It cannot interact with the desktop. | ||
===Manual startup=== | ===Manual startup=== | ||
The service start type specifies the service's behavior after reboot: | |||
* Auto - Automatically start after a re-boot | * Auto - Automatically start after a re-boot | ||
Line 272: | Line 264: | ||
* Disabled - Deactivated | * Disabled - Deactivated | ||
We want our script to start when the PC is powered up. Either change the Manual state to Auto using service manger | We want our script to start when the PC is powered up. Either change the Manual state to Auto using service manger, or | ||
use a script to set the service state. Create a new file named '''start_automatic.vbs''' with the following content: | |||
<pre> | <pre> | ||
Line 289: | Line 281: | ||
For testing this script starts our service and changes it to Automatic. | For testing, this script starts our service and changes it to Automatic. | ||
<pre> | <pre> | ||
Line 308: | Line 300: | ||
</pre> | </pre> | ||
==Simulating service program crash== | ==Simulating service program crash== | ||
Our batch file is ideal for simulating a service program crash and demonstrating recovery. The batch file only runs once (simulating a program crash), srvstat can be set to automatically restart the program using directive '''auto_restart=y''', setting '''restart_interval=seconds''' will pause before starting. | Our batch file is ideal for simulating a service program crash and demonstrating recovery. The batch file only runs once (simulating a program crash), srvstat can be set to automatically restart the program using directive '''auto_restart=y''', setting '''restart_interval=seconds''' will pause before starting. | ||
Line 329: | Line 320: | ||
|} | |} | ||
'''Note 1:''' auto_restart will restart the service program if it exits for any reason. If restart_interval is defined, then before restarting, SRVSTART will wait | '''Note 1:''' auto_restart will restart the service program if it exits for any reason. If restart_interval is defined, then before restarting, SRVSTART will wait the specified seconds. | ||
'''Note 2:''' auto_restart does not | '''Note 2:''' auto_restart does not restart the service program if it is stopped by request (eg NET STOP or Control Panel|Services). | ||
'''Note 3:''' auto_restart does not restart the service program if it thinks that Windows NT is shutting down. If you are irritated by services restarting during Windows NT shutdown, then increase the value of restart_interval to, say, a minute. | '''Note 3:''' auto_restart does not restart the service program if it thinks that Windows NT is shutting down. If you are irritated by services restarting during Windows NT shutdown, then increase the value of restart_interval to, say, a minute. | ||
==Conclusion== | ==Conclusion== | ||
The above has shown how to run a simple VBScipt as a service. | The above has shown how to run a simple VBScipt as a service. | ||
When Uniform Server is installed as a permanent installation concepts outlined above can be used for kicking portable cron into life. | When The Uniform Server is installed as a permanent installation, the concepts outlined above can be used for kicking portable cron into life. | ||
Portable cron is intended to be run from a memory stick and still requires manually starting. | Portable cron is intended to be run from a memory stick and still requires manually starting. | ||
==Where to next== | ==Where to next== | ||
Related to the above service is Uniform Server Cron | Related to the above service is The Uniform Server Cron. Both portable cron and running as a service have been implemented. For the details, see [[Coral: cron intro|Cron]]. | ||
---- | ---- | ||
[[Category: Uniform Server 8-Coral]] | [[Category: Uniform Server 8-Coral]] |
Latest revision as of 16:18, 14 November 2011
CRON - SrvStart utility tutorial This short tutorial covers using the SrvStart utility written by Nick Rozanski. It allows you to run a script as a service! What is a Service?
It's not a trivial exercise, but this complexity is hidden when using SrvStart.
|
|
Download SrvStart
Download SRVStart.exe from http://www.rozanski.org.uk/software
- Navigate to section SRVSTART.EXE
- Click on the image named "Executable"
- This starts the download.
- Save file srvstart_run.v110.zip to any folder.
- Extract contents of this file to where you saved it.
Install SRVStart
The folder srvstart_run.v110 contains the following files:
- logger.dll (28KB)
- srvstart.dll (140KB)
- srvstart.exe (size 36KB)
- svc.exe
We are only interested in files srvstart.exe, srvstart.dll and logger.dll
For this tutorial, create a new folder c:\service_test and copy the above three files into it.
Note 1:
The documentation says to copy files srvstart.exe, srvstart.dll, logger.dll and msvcrt.dll to C:\WINDOWS\system32 (system path). For XP onwards msvcrt.dll is already installed on a system path. The other three files do not need to be installed on a system path; see below.
We are going to run srvstart.exe from its installation folder. This allows it to be portable by not installing anything on a host PC (assumes running from memory stick).
Note 2:
Notwithstanding the above, when installing a service, it does write to the registry, so you must remember to stop and uninstall the service you create if it resides on an external drive or a memory stick.
Script to run
For this tutorial we require a script to run. Create a new text document named my_script.vbs with the following content:
Set oWS = WScript.CreateObject("WScript.Shell") oWS.Run "%comspec% /c echo " & Chr(07), 0, True Set oWS = WScript.CreateObject("WScript.Shell") oWS.Run "%comspec% /c echo " & Chr(07), 0, True |
This is an annoying VBScript which just produces beeps when it runs. Double click on my_script.vbs and confirm that it produces two beeps. |
Limitation
An application to be run as a service must have one of the following file extensions: .exe, .com or .bat. The script we want to run has an extension of .vbs so it cannot be run directly.
At first this looks very restrictive, however a batch file will run any application. To run our script as a service, create a batch file named my_script.bat with the following content:
start my_script.vbs |
start Runs our vb script and closes. It does not wait for our vb script to finish. |
Note 1: Running my_script.bat a command window opens for a short time. When run as a service this is hidden.
Note 2: Removing the "start" keyword, our vb script runs and the command window remains open until vb script finishes. Again, when run as a service, this is hidden.
SrvStart configuration file
To create our application (script) service SrvStart requires a configuration file.
- Each service to be run is contained in a separate block.
- Every block starts with a service name enclosed in square brackets.
- Each block must contain a startup command, which specifies the program (application/script) to launch.
Create a file named srvstart.ini with the following content:
[z_test] startup=C:\service_test\my_script.bat startup_dir=C:\service_test |
Service name is z_test |
Control_batch_files
We are now ready to install and run our service. This can be done using a command prompt and issuing the appropriate commands. I prefer to put these commands into separate batch files as follows:
z_install.bat
Create a file named z_install.bat with the following content: srvstart.exe install z_test -c C:\service_test\srvstart.ini pause |
|
z_uninstall_service.bat
Create a file named z_uninstall_service.bat with the following content: srvstart.exe remove z_test pause |
|
z_service_start.bat
Create a file named z_service_start.bat with the following content: net start z_test pause |
|
z_service_stop.bat
Create a file named z_service_stop.bat with the following content: net stop z_test pause |
|
Test - Perceived problems
Run z_install.bat |
You will see "Successfully created non-desktop service 'z_test'" |
Run z_service_start.bat |
Confirm two beeps are produced. Note the following messages:
With the confirmation of two beeps, our script was successfully run. The reason for could not be started message; the batch file closes immediately when run. Srvstart regularly checks the running status of an application it started, it immediately sees the script is no longer running and reports back to the service control that it could not be started. The service did not report an error because it’s not an error. |
Run z_service_stop.bat |
Note the following messages
The batch file is not running, hence the service is reported as not running; see above. The above is not a problem; it's the way we want to run our script. |
Run z_uninstall_service.bat |
Note the following message
Confirms correct operation |
Note: The above results may be a little confusing. Rremember our VBScript does not run continuously; it's a run-once program. Its purpose is to familiarise yourself with the SrvStart program.
Service Status
With the service created, it will now appear in the Windows Services list and can be configured like any other Windows Service. The service which is installed has the following characteristics.
- Its short name and display name are both z_test.
- It is set to Manual startup.
- It starts using LocalSystem and has no dependencies.
- It cannot interact with the desktop.
Manual startup
The service start type specifies the service's behavior after reboot:
- Auto - Automatically start after a re-boot
- Manual - Need to manually start service once PC started
- Disabled - Deactivated
We want our script to start when the PC is powered up. Either change the Manual state to Auto using service manger, or use a script to set the service state. Create a new file named start_automatic.vbs with the following content:
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2") Set colRunningServices = objWMIService.ExecQuery ("Select * from Win32_Service") For Each objService in colRunningServices If objService.DisplayName = "z_test" Then errReturn = objService.Change( , , , , "Automatic") End If Next
The above script will change the service state to Automatic. After you have installed your service it only requires running once.
For testing, this script starts our service and changes it to Automatic.
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2") Set colRunningServices = objWMIService.ExecQuery ("Select * from Win32_Service") For Each objService in colRunningServices If objService.DisplayName = "z_test" Then If objService.State = "Running" Then MsgBox "Is Running" End If If objService.State= "Stopped" Then errReturn = objService.StartService() errReturn = objService.Change( , , , , "Automatic") End If End If Next
Simulating service program crash
Our batch file is ideal for simulating a service program crash and demonstrating recovery. The batch file only runs once (simulating a program crash), srvstat can be set to automatically restart the program using directive auto_restart=y, setting restart_interval=seconds will pause before starting.
This can be put to good use; it allows a one time run script to be periodically called. Modify the configuration file as shown below:
[z_test] startup=C:\service_test\my_script.bat startup_dir=C:\service_test auto_restart=y restart_interval=10 |
Our test script will now be run every 10 seconds |
Note 1: auto_restart will restart the service program if it exits for any reason. If restart_interval is defined, then before restarting, SRVSTART will wait the specified seconds.
Note 2: auto_restart does not restart the service program if it is stopped by request (eg NET STOP or Control Panel|Services).
Note 3: auto_restart does not restart the service program if it thinks that Windows NT is shutting down. If you are irritated by services restarting during Windows NT shutdown, then increase the value of restart_interval to, say, a minute.
Conclusion
The above has shown how to run a simple VBScipt as a service. When The Uniform Server is installed as a permanent installation, the concepts outlined above can be used for kicking portable cron into life. Portable cron is intended to be run from a memory stick and still requires manually starting.
Where to next
Related to the above service is The Uniform Server Cron. Both portable cron and running as a service have been implemented. For the details, see Cron.