WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Run Winbatch as a Service

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

A NT Service Template Script - For Beginners.txt

Sample service script template

;This revision required 2005B or newer.

;Initialze variables for the SvcSetAccept function
SERVICE_ACCEPT_STOP             =   1    ;The service can be stopped
SERVICE_ACCEPT_PAUSE_CONTINUE   =   2    ;The service can be paused and continued
SERVICE_ACCEPT_SHUTDOWN         =   4    ;The service is notified when system shutdown occurs
SERVICE_ACCEPT_LOGOFF           =   32768;The service is notified when user logoff occurs

;Initialize variables for the SvcSetState function
SERVICE_STATE_STOPPED           =   1   ;The service is not running
SERVICE_STATE_START_PENDING     =   2   ;The service is initializing
SERVICE_STATE_STOP_PENDING      =   3   ;The service is stopping
SERVICE_STATE_RUNNING           =   4   ;The service is running
SERVICE_STATE_CONTINUE_PENDING  =   5   ;The service continue is pending
SERVICE_STATE_PAUSE_PENDING     =   6   ;The service pause is pending
SERVICE_STATE_PAUSED            =   7   ;The service is paused

;Initialize variables for the SvcWaitForCmd function
SERVICE_CONTROL_NOT_SERVICE     =  -1   ;Script not running as a service
SERVICE_CONTROL_TIMEOUT         =   0   ;Timeout occurred or no codes to process

SERVICE_CONTROL_STOP            =   1   ;Requests the service to stop
SERVICE_CONTROL_PAUSE           =   2   ;Requests the service to pause
SERVICE_CONTROL_CONTINUE        =   3   ;Requests the paused service to resume
SERVICE_CONTROL_SHUTDOWN        =   5   ;Requests the service to perform

                                        ;cleanup tasks, because the system is shutting down                                        ;is shutting down

SERVICE_CONTROL_USER128         = 128   ;User command 128
SERVICE_CONTROL_USER129         = 129   ;User command 129
SERVICE_CONTROL_USER130         = 130   ;User command 130
SERVICE_CONTROL_USER131         = 131   ;User command 131

;                                       ;More user commands as needed
SERVICE_CONTROL_USER255         = 255   ;User command 255
SERVICE_CONTROL_LOGOFF          = 32768 ;logoff notification

;Create working directory if it does not exist
if !DirExist("c:\svcs") then  DirMake("c:\svcs")
inicontrolfile="c:\svcs\svc.ini"

IniWritePvt("Control","Request",0,inicontrolfile)   ; 0=No Request

;Setup debugging prompt strings....
debugcodes="0: Timeout|1: Stop|2: Pause|3: Continue|5: Shutdown|128: User Cmd 128|129:User Cmd 129|32768: Logoff"

;Tell system that we want all notifications
flag=svcSetAccept( SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_LOGOFF )
if flag== -1 
   DoingDebug=@TRUE
   Pause("Debug Mode","Not currently running as a service")
else
   DoingDebug=@FALSE
   ;Set up error handling
   IntControl(12,2+8,0,0,0)                    ;Tell WinBatch to not honor terminate and 
                                               ;not complain on Windows exit
   IntControl(38,1,"C:\svcs\errorlog.txt",0,0) ;Route fatal errors to a log file
endif

;Now for the main service loop
;in this service we respond to all control messages 
;and check an ini file for work every 5 seconds
BoxOpen("Initializing","main service loop")

SvcSetState(SERVICE_STATE_RUNNING)

while @TRUE
   if DoingDebug==@FALSE
      code=SvcWaitForCmd(5000)      ; Timeout in 5 seconds
   else
       ;For Debugging.  Prompt tester to see what code should be pretended here
       code=AskItemList("Service Debug",debugcodes,"|",@UNSORTED,@SINGLE)
       if code=="" then continue
       code=ItemExtract(1,code,":")
   endif

   switch code

          case SERVICE_CONTROL_TIMEOUT       
               ;Timeout occurred
               BoxText("TimeoutOccured")

               ;Read INI
               inivalue=IniReadPvt("Control","Request",0,inicontrolfile)

               if inivalue!=0
                  IniWritePvt("Control","Request",0,inicontrolfile)
                  gosub DoIniWork
               endif
               break

          case SERVICE_CONTROL_STOP          
               ;Stop command received
               BoxText("Stop command received")
               SvcSetState(SERVICE_STATE_STOP_PENDING)
               ;do stop cleanup work here

               timedelay(5)
               SvcSetState(SERVICE_STATE_STOPPED)
               exit    ;Goodbye
               break

          case SERVICE_CONTROL_PAUSE         
               ;Pause command received
               BoxText("Pause command received")
               SvcSetState(SERVICE_STATE_PAUSE_PENDING)

               ;do pause cleanup work here
               SvcSetState(SERVICE_STATE_PAUSED)
               break

          case SERVICE_CONTROL_CONTINUE 
               ;Continue command received
               BoxText("Continue command received")
               SvcSetState(SERVICE_STATE_CONTINUE_PENDING)
               ;do resume from pause state initilization here
               SvcSetState(SERVICE_STATE_RUNNING)
               break

          case SERVICE_CONTROL_SHUTDOWN      
               ;Shutdown notification received
               ;Approx. 20 seconds to process
               BoxText("Shutdown notification received")

               SvcSetState(SERVICE_STATE_STOP_PENDING)
               ;do stop cleanup work here
               SvcSetState(SERVICE_STATE_STOPPED)
               exit    ;Goodbye
               break

          case SERVICE_CONTROL_USER128       

               ;User command 128 received
               BoxText("User command 128 received")
               break

          case SERVICE_CONTROL_USER129
               ;User command 129 received
               BoxText("User command 129 received")
               break
    
          case SERVICE_CONTROL_LOGOFF
               ;Logoff command received
               BoxText("Logoff command received")
               break

         case code
               ;Unrecognized command received
               BoxText("Unrecognized command received")
               break
      endswitch

endwhile

;Note.  The preceeding loop never exits
exit   ; Just a formality

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;The DoIniWork subroutine is designed to allow a WinBatch script or
;other process running under the user account to "flag" the service
;to perfrom useful work of a kind the user is not privileged to do.
;In this case, perhaps just setting a registry key.

:DoIniWork
   RegSetValue(@REGMACHINE,"Software\Test\WinBatch Services[test1]","It Worked")
return


See the WinBatch.hlp file for more details...
Article ID:   W13494
Filename:   A NT Service Template Script - For Beginners.txt
File Created: 2006:01:27:12:10:14
Last Updated: 2006:01:27:12:10:14