Changes

Jump to navigation Jump to search

Coral: cron portable design

9,175 bytes added, 13:46, 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 - Portable Design</span>

Running scripts as a service is easy to implement using Rozanski's utility see the following page [[Coral: cron srvstart utility|srvStart utility tutorial]] for details.

Uniform Server has this utility integrated into Coral’s control architecture allowing portable Cron to run as a service. For anyone wanting to modify or understand how it has been implemented the following describe its operation and design.


__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]]
|}

|}

==Portable Cron==
Portable Cron consists of an infinite program loop. It periodically checks an external file to determine when to exit. This concept is important when running the script as a service.

==Service files==
Cron service files are contained in folder UniServer\uni_con\'''cron_service''' and perform the following function:

'''Utility program files''':

* logger.dll - Service utility
* srvstart.dll - Service utility program functions
* srvstart.exe - Service utility program
* srvstart.ini - Service utility configuration file

'''Individual service control''':

* z_install.bat - Install a named service
** start_automatic.vbs - Starts service and changes service run status to Automatic
* z_service_start.bat - Starts service independently of changing run status.
* z_service_stop.bat - Stop service
* z_uninstall_service.bat - Remove service

'''Combined service control''':

* install_start_service.bat - Uses the above to install and start cron service
* stop_uninstall_service.bat - Uses the above to stop and remove service.

Following provides details for each control file.

'''''[[#top | Top]]'''''
==File description and function==
For clarity only pertinent information is included (files title block has been removed)

===srvstart.ini===
This file allows you to specify any number of services to run. Each service is specified in a block starting with a unique name enclosed in square brackets. Uniform Server Cron requires a single block as shown below:


{|
|-
|
<pre>
[US_CronS1]
startup=C:\UniServer\uni_con\cron\start_cron.bat
startup_dir=C:\UniServer\uni_con\cron
shutdown=C:\UniServer\uni_con\cron\stop_cron.bat
auto_restart=y
restart_interval=30
</pre>
|
* '''[US_CronS1]''' specifies a unique service name '''US_CronS1''' When running multi-servers digit is incremented to retain uniquness.
* '''startup''' Full path to the script to run. Service can run only .exe, .com or .bat files hence start_cron.bat This in turn runs run_cron.vbs (portable cron)
* '''startup_dir''' Initially look in this folder for the file to run
* '''auto_restart''' If portable Cron fails attempt to restart it after a delay of 30 seconds.
* '''restart_interval''' Define restart delay time, attempt to re-start script every 30 seconds
|}
'''shutdown'''
When the service control manger (SCM) receives a '''net stop''' message it runs the script (stop_cron.bat) defined by '''shutdown'''.


Script '''stop_cron.bat''' runs script '''stop_cron.vbs''' which sets cron status to '''stop''' in configuration file '''config_tracker.ini''' this signals portable Cron to stop. The batch file '''start_cron.bat''' then closes informing SCM service has stopped.

===z_install.bat===
The service utility installs a named service (US_CronS1) using the '''install''' command. It uses a configuration "'''-c'''" file defined with an absolute path to prevent any ambiguity. Configuration file used is srvstart.ini

<pre>
rem ### working directory current folder
pushd %~dp0
srvstart.exe install US_CronS1 -c C:\UniServer\uni_con\cron_service\srvstart.ini
: pause

rem ### restore original working directory
popd
</pre>

'''Note 1:''' The above installs the service as a manual service. After re-starting your PC you must manually start the service next script changes service state to automatic.

'''Note 2:'''

* pushd %~dp0 - Saves current working directory and change working directory to batch file location.
* popd - Restores original working directory

This allows a batch file to be run from any location without worrying about current working directory

'''Note 3:''' The pause command is used to pause a batch file. It allows any errors to be seen, waits for a user to press any key.
It is used only for testing hence is disabled by commenting it out using a colon ":"


===start_automatic.vbs===
The service utility installs a service as a manual service this state requires changing to automatic allowing the service script to run when the PC is restarted. The following script starts the service and changes state 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 = "US_CronS1" Then
'If objService.State = "Running" Then
' MsgBox "Is Running"
'End If
If objService.State= "Stopped" Then
errReturn = objService.StartService() 'Start service
errReturn = objService.Change( , , , , "Automatic") 'Change state
End If
End If
Next
</pre>
|
Create a Windows Management Instrumentation (WMI)object.


Executes a query on the win32 service object


Checks for named service running. Starts it if not running.


Changes service state to Automatic
|}
The above script is run after installing a service.

===z_service_start.bat===
This script is provided for testing. It starts a service that has been installed with a manual status.

{|
|-
|
<pre>
rem ### working directory current folder
pushd %~dp0
net start US_CronS1
:pause
rem ### restore original working directory
popd
</pre>
|
The script uses the standard '''net start''' command to start a named (US_CronS1) service.
|}

===z_service_stop.bat===
This script stops a named service.

{|
|-
|
<pre>
rem ### working directory current folder
pushd %~dp0
net stop US_CronS1
:pause
rem ### restore original working directory
popd
</pre>
|
The script uses the standard '''net stop''' command to stop a named (US_CronS1) service.
|}

===z_uninstall_service.bat===
The service utility uninstalls a named service (US_CronS1) using the uninstall command.

{|
|-
|
<pre>
rem ### working directory current folder
pushd %~dp0
srvstart.exe remove US_CronS1
:pause
rem ### restore original working directory
popd
</pre>
|
To use this script service must first be stopped.
|}


===install_start_service.bat===
This script combines above scripts (z_install.bat and start_automatic.vbs) into a single script, which is used by Coral’s control interface.

{|
|-
|
<pre>
rem ### working directory current folder
pushd %~dp0
:Install srvice
call z_install.bat
:Change to automatic and start service
call start_automatic.vbs
: pause
rem ### restore original working directory
popd
</pre>
|
'''Call''' Runs and waits for a script to complete before continuing
|}

===stop_uninstall_service.bat===
This script combines above scripts (z_service_stop.bat and z_uninstall_service.bat) into a single script, which is used by Coral’s control interface.

<pre>
rem ### working directory current folder
pushd %~dp0
:Stop service
call z_service_stop.bat
:Uninstall service
call z_uninstall_service.bat
: pause
rem ### restore original working directory
popd
</pre>

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

==Server relocation==
Relocating the servers all absolute paths are re-written the following files are changed accordingly:

* srvstart.ini
* z_install.bat

==Multi-Servers==
Running multi-servers requires unique service names hence these files are changed accordingly:

* srvstart.ini
* z_install.bat
* start_automatic.vbs
* z_service_start.bat
* z_service_stop.bat
* z_uninstall_service.bat


'''''[[#top | Top]]'''''
==Where to next==

[[Coral: cron srvstart utility|CRON - SrvStart utility tutorial]]

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

----

[[Category: Uniform Server 8-Coral]]

Navigation menu