BATCH: Check, START and STOP

Use this forum if you have problems with a hMailServer script, such as hMailServer WebAdmin or code in an event handler.
Post Reply
User avatar
Dravion
Senior user
Senior user
Posts: 1053
Joined: 2015-09-26 11:50
Location: Germany
Contact:

BATCH: Check, START and STOP

Post by Dravion » 2018-12-06 12:26

Little a helper for hMailServer Service handling

1) Checks the Service state
2) Returns Sucess or Fail on Service operation
3) Surpresses SC noisy messages with >nul

File: sc_start.bat

Code: Select all

@REM - Check if hMailServer is stopped and if it is, start the Service
@ECHO OFF
@for /F "tokens=3 delims=: " %%H in ('sc query "hMailServer" ^| findstr "STATE"') do (
  if /I "%%H" NEQ "RUNNING" (
   @ECHO hMailServer Service is NOT running, starting it...
   @sc start "hMailServer" >nul
   if "%ERRORLEVEL%"=="0" (
		@ECHO SUCCESS: Service was started, go ahead.
	) else (
		@ECHO WARNING: Service could NOT be started.
	)
  )
)
File: sc_stop.bat

Code: Select all

@REM - Check if hMailServer is stopped and if it is, stop the Service
@ECHO OFF
@for /F "tokens=3 delims=: " %%H in ('sc query "hMailServer" ^| findstr "STATE"') do (
  if /I "%%H" EQU "RUNNING" (
   @ECHO hMailServer Service is running, stopping it...
   @sc stop "hMailServer" >nul
   if "%ERRORLEVEL%"=="0" (
		@ECHO SUCCESS: Service was stopped, go ahead.
	) else (
		@ECHO WARNING: Service could NOT be stopped.
	)
  )
)
Screenshot
sc_start_sc_stop.png
My x64-Bit builds of hMailserver

hMailServer-5.6.8-B2426-x64.msi (LibreSSL)
hMailServer-5.6.8-B2426-x64.exe (OpenSSL)

Downloads:
https://github.com/hMailServer-ComDevs/hmailserver/releases

Post Reply