Coral: cron portable design

From The Uniform Server Wiki
Revision as of 15:22, 14 November 2011 by BobS (talk | contribs) (Punctuation and grammatical changes.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

CRON - Portable Design


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


UniServer 8-Coral
  Home
  Quick Start
  General
  Apache
  MySQL
  PHP
  MSMTP
» CRON
  DtDNS
  Db Backup
  Perl
  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.

The following provides details for each control file.

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. The Uniform Server Cron requires a single block as shown below:


[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
  • [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

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

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.

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

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.

rem ### working directory current folder 
pushd %~dp0
net start US_CronS1
:pause
rem ### restore original working directory
popd

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.

rem ### working directory current folder 
pushd %~dp0
net stop US_CronS1
:pause
rem ### restore original working directory
popd

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.

rem ### working directory current folder 
pushd %~dp0
srvstart.exe remove US_CronS1
:pause
rem ### restore original working directory
popd

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.

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

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.

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


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


Where to next

Running scripts as a service is easy to implement using Rozanski's utility. The next page CRON - SrvStart utility tutorial has details about this utility.