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.

Run Winbatch as a Service Sample Code

Keywords: 	 Run Winbatch as a Service

Note: Use these instructions, running Winbatch as a service utilizing the SRVANY.EXE, if you have versions of Winbatch prior to 2001.

For 2001 versions of Winbatch, see the functions in the Winbatch.HLP file, to run Winbatch as a native mode service. See Article W13494.

     configuring winbatch to run as a service
  ----------------------------------------------

  these notes describe installing a service which will run a
   winbatch script at service startup.

  * items you must have on hand before starting:

   1) a compiled and debugged winbatch script which can be run as a
       service. in this example, the script is:  c:\temp\wbtsrv.exe

       following these notes are two samples of wbt scripts which
       can be run under a service.

   2) one copy of any NT resource kit which contains the program
       'srvany.exe'. the program srvany must be on the local machine -
       it cannot be installed from a networked drive.


   * instructions:

   1)  run instsrv to install the service, giving it a made-up name.
        in this case, the locally-picked name is winbsrv. from a dos
        prompt (or command line) enter this command:

        instsrv winbsrv  c:\temp\srvany.exe

        should get msg:  "the service was successfully added"
        note: to uninstall the service: instsrv winbsrv remove


    2)  run regedt32 to edit the registry in order to specify which
         winbatch script you want run when 'winbsrv' starts'. from a
         dos prompt or command line:

         regedt32
         go to: HKLM\system\CCS\services\winbsrv

        select edit/add key and make the key name 'parameters'
           leave class blank
        select the 'parameters' key, and click on edit/add value,
           for the 'value name', enter: application
           for the  'data type', enter: REG_SZ    (probably the default)
              click on ok, and the 'string editor' box will appear
           for the 'string',     enter: c:\temp\wbtsrv.exe


    3)  go to the services panel, and select winbatch and 'startup'.
         make sure the 'allow to interact with desktop' attribute is set
         if anything in the script or called code requires it. click
         on 'start', and the service should start up.

         note: on the way to debugging this process several errors
                were encountered, and each one of them had entries
                describing the error and how to handle it in technet.


 =====================================================================
    here are some samples of code running winbatch as a service:

 =====================================================================
 ;
 ;
 ;  This version of a winbatch script picked up and run by
 ;   the srvany process will:
 ;      * wake up at 300 second intervals
 ;      * check to see if files c:\temp\wbc, exe, or bat exist
 ;      * if any of these exists, it will be executed and
 ;         then renamed
 ;

 AddExtender("wwwnt32i.dll")

 IntControl(1001,1,0,0,0)   ;  1 = set to run as service, don't
 ;                              log stop service when user logs off

 :oncemore
   timedelay(300)
   if FileExist("c:\temp\xxx.wbc")
     call("c:\temp\xxx.wbc","")
     FileRename("c:\temp\xxx.wbc","c:\temp\xxx.wasrun.wbc")
   endif

   if FileExist("C:\temp\xxx.exe")
     run("c:\temp\xxx.exe","")
     FileRename("c:\temp\xxx.exe","c:\temp\xxx.wasrun.exe")
   endif

   if FileExist("C:\temp\xxx.bat")
     cmd = "c:\temp\xxx.bat"
     runhidewait("cmd",'cmd /c "%cmd%"')
     FileRename("c:\temp\xxx.bat","c:\temp\xxx.wasrun.bat")
   endif

 goto oncemore


 ====================================================================
 ;
 ;
 ;  This version of a winbatch script picked up and run by
 ;   the srvany process will:
 ;      * wait 120 seconds after the wbtsrv service is started
 ;      * check to see if the service 'spooler' is started
 ;      * if not, start it.
 ;      * go into a loop and check every 300 seconds to see
 ;         if the spooler is still started. if not, restart it
 ;

 AddExtender("wwwnt32i.dll")

 IntControl(1001,1,0,0,0)      ;  1 = set to run as service, don't
 ;                                log stop service when user logs off
 sysinfo  = WinSysInfo()                     ; local system
 sysname1 = ItemExtract(1,sysinfo,@tab)      ; local system name
 sysname  = StrCat("\\",sysname1)


   timedelay(120)

 :startrecycle                         ; is the service started:
   statesplr =  wntSvcStatus(sysname,"Spooler", 0, 2)

   if statesplr == "1"
     wntSvcStart(system, "Spooler", 0, "", "")
   endif

   timedelay(100)
   goto startrecycle




Article ID:   W14947
File Created: 2001:11:08:12:41:06
Last Updated: 2001:11:08:12:41:06