The following quick and dirty test script does accomplish the above, but the service console then fails to get back a handle on the service
@echo off
@rem ---------------------------------------------------------------------------
@rem Usrdata
@rem ---------------------------------------------------------------------------
set SVCNAM=OurService
set PROCJAV=javaw.exe
set PROCSVC=Mendix.Service.exe
set INTERV=1
@rem ---------------------------------------------------------------------------
@rem Main
@rem ---------------------------------------------------------------------------
goto monitor
@rem ----------------------------------------------------------------------------
@rem Monitor Loop
@rem ----------------------------------------------------------------------------
:monitor
call :prgcheck
timeout /t %INTERV% >NUL
goto monitor
@rem ----------------------------------------------------------------------------
@rem Check Processes
@rem ----------------------------------------------------------------------------
:prgcheck
tasklist /FI "IMAGENAME eq %PRGNAM%" 2>NUL | find /I /N "%PRGNAM%">NUL
if "%ERRORLEVEL%"=="0" (
echo %time%: Mendix Running
) else (
echo %time%: Mendix Not Running : Restarting...
@rem -----------------------------------------------------------------------------
@rem Kill
@rem -----------------------------------------------------------------------------
taskkill /f /im %PROCSVC% > nul 2>&1
taskkill /f /im %PROCJAV% > nul 2>&1
@rem -----------------------------------------------------------------------------
@rem Start
@rem -----------------------------------------------------------------------------
net start %SVCNAM%
)
I created this batch file script to wait until the service has started:
SET servicename=yourservice
SET servicestarted=1
for /F "tokens=3 delims=: " %%H in ('sc query "%servicename%" ^| findstr " STATE"') do (
if /I "%%H" NEQ "RUNNING" (
if /I "%%H" NEQ "START_PENDING" (
ECHO Starting %servicename%
sc start "%servicename%" > nul
REM SC does not wait for successfully starting the service.
SET /a servicestarted=0
)
)
)
:checkstarted
if %servicestarted% EQU 0 (
for /F "tokens=3 delims=: " %%H in ('sc query "%servicename%" ^| findstr " STATE"') do (
if /I "%%H" EQU "RUNNING" (
SET /a servicestarted=1
)
)
timeout /t 1 > nul
goto checkstarted
)
ECHO %servicename% started.
You have to start this script as administrator to work.