Changes

Jump to navigation Jump to search

Coral: cron srvstart utility

12,389 bytes added, 13:47, 12 November 2011
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..."
<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-size:12px">'''''Coral'' :'''&nbsp; [[Coral: cron intro|CRON]] | [[Coral: cron configuration detail|CRON - Configuration detail]] | [[Coral: cron portable design|CRON - Portable Design]] | [[Coral: cron srvstart utility|CRON - SrvStart utility tutorial]]</span></div>

{|style="margin-top:5px;"
|-valign="top"
|
<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!

'''What is a Service?'''<br />
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 />
Programs (application) designed to run as a service contain code that interface 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 scripts status.

Not a trivial exercise however this complexity is hidden when using SrvStart.


__TOC__

|
{| cellpadding="0" cellspacing="0" style="margin-left:6px;margin-bottom:5px;padding-left:6px;padding-right:6px;font-size:11px;background-color: #FFFFEB;border: 1px solid darkslateblue;"
|-
|colspan="2" style="font-weight:bold;"| UniServer&nbsp;8-Coral
|-
|&nbsp;||[[Coral: Introduction|Home]]
|-
|&nbsp;||[[Coral: start quick start|Quick Start]]
|-
|&nbsp;||[[Coral: general intro|General]]
|-
|&nbsp;||[[Coral: apache intro|Apache]]
|-
|&nbsp;||[[Coral: mysql intro|MySQL]]
|-
|&nbsp;||[[Coral: php intro|PHP]]
|-
|&nbsp;||[[Coral: msmtp intro|MSMTP]]
|-
|<span style="font-size: 14px;">»</span>||[[Coral: cron intro|CRON]]
|-
|&nbsp;||[[Coral: dtdns intro|DtDNS]]
|-
|&nbsp;||[[Coral: dbbackup intro|Db Backup]]
|-
|&nbsp;||[[Coral: perl intro|Perl]]
|-
|&nbsp;||[[Coral: index main|Main Index]]
|}
|}

==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 interest only 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.


'''Note 1:'''<br />
The documentation states 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. Other three files need not 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:'''<br />
When installing a service it does write to the registry hence remember to stop and uninstall the service you create especially if running from a memory stick.


'''''[[#top | Top]]'''''
==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:

{|
|-
|
<pre>
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
</pre>
|
This is an annoying VBScript it produces beeps when run.


Double click on my_script.vbs and confirm it produces two beeps.
|}

'''''[[#top | Top]]'''''
===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 hence 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:

{|
|-
|
<pre>
start my_script.vbs
</pre>
|
'''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.
|}

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


'''''[[#top | Top]]'''''
==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:

{|
|-
|
<pre>
[z_test]
startup=C:\service_test\my_script.bat
startup_dir=C:\service_test
</pre>
|
Service name is '''z_test''' <br />
'''startup''' Full path to script <br />
'''startup_dir''' Folder to start looking in
|}

'''''[[#top | Top]]'''''
==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:
<pre>
srvstart.exe install z_test -c C:\service_test\srvstart.ini
pause
</pre>
|
* srvstart.exe - Run srvstart program
* install - Command to run is install
* z_test - Name of service to install
* -c Configuration file to use
|}

===z_uninstall_service.bat===
{|
|-
|
Create a file named '''z_uninstall_service.bat''' with the following content:
<pre>
srvstart.exe remove z_test
pause
</pre>
|
* srvstart.exe - Run srvstart program
* remove - Command to run is install
* z_test - Name of service to uninstall
|}

===z_service_start.bat===
{|
|-
|
Create a file named '''z_service_start.bat''' with the following content:
<pre>
net start z_test
pause
</pre>
|
* net - Command is used to manage services
* start - Start the named service
* z_test - Name of service to start
|}

===z_service_stop.bat===
{|
|-
|
Create a file named '''z_service_stop.bat''' with the following content:
<pre>
net stop z_test
pause
</pre>
|
* net - Command is used to manage services
* stop - Stops the named service
* z_test - Name of service to start
|}

'''''[[#top | Top]]'''''
==Test - Perceived problems==
{|
|-valign="top"
|
Run '''z_install.bat'''
|
You will see "Successfully created non-desktop service 'z_test'"
|-valign="top"
|
Run '''z_service_start.bat'''
|
Confirm two bleeps are produced. Note the following messages
* The z_test service is starting.
* The z_test service could not be started.
* The service did not report an error.
* More help is available by typing NET HELPMSG 3534.
With the confirmation of two beeps our script was successfully run. Reason for '''could not be started message'''; the batch file closes immediately when run. Srvstart regularly checks running status of an application it started, it immediately sees the script no longer running and reports back to the service control it could not be started.
The service did not report an error because it’s not an error.
|-valign="top"
|
Run '''z_service_stop.bat'''
|
Note the following messages
* C:\service_test>net stop z_test
* The z_test service is not started.
* More help is available by typing NET HELPMSG 3521.
The batch file is not running hence the service as reported is not running see above.
Above is not a problem its the way we want to run our scrip.
|-valign="top"
|
Run&nbsp;'''z_uninstall_service.bat'''&nbsp;
|
Note the following message
* Deletion of service 'z_test' succeeded
Confirms correct operation
|}
'''Note:''' The above results may be a little confusing, remember our VBScript does not run continuously its a run once program. Its purpose is to familiarise yourself with the Srvstart program.

'''''[[#top | Top]]'''''
==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.
* If cannot interact with the desktop.

===Manual startup===
Service start type specifies the services 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.
Alternatively use a script to set the service state. Create a new file named '''start_automatic.vbs''' with the following content:

<pre>
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
</pre>

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.

<pre>
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
</pre>

'''''[[#top | Top]]'''''
==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:

{|
|-
|
<pre>
[z_test]
startup=C:\service_test\my_script.bat
startup_dir=C:\service_test
auto_restart=y
restart_interval=10
</pre>
|
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 seconds 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.

'''''[[#top | Top]]'''''
==Conclusion==

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.
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 Uniform Server Cron both portable cron and running as a service have been implemented for details see [[Coral: cron intro|Cron]].



'''''[[#top | Top]]'''''

----

[[Category: Uniform Server 8-Coral]]

Navigation menu