We need to control a daemon like start
, stop
, restart
, status
using batch script where CKI_SYSTEM_STAT
is variable, changed dynamically base on the condition of the daemon. Here we have to simulate it for your realization according to the issue. CKI_SYSTEM_STAT
changed randomly, but inside the for
loop it remain unchanged under stop
label.
For this reason we are unable to detect either the daemon stopped successfully when stop operation performed. Here is the batch script for your kind consideration.
rem ebisd.bat
@echo off
if "%OS%" == "Windows_NT" setlocal
set "CKI_OPTION_NAME=%1"
call :init_ebis
call :ebis_case
goto :eof
:init_ebis
rem initialization of varible
if "%CKI_DAEMON_NAME%" == "" set "CKI_DAEMON_NAME=Demon"
goto :eof
rem fetch the process info
:init_meta
rem init flag
SET /A CKI_RANDOM_NUMB=%RANDOM%%%10
echo Random Number %CKI_RANDOM_NUMB%
if %CKI_RANDOM_NUMB% lss 7 (
set "CKI_SYSTEM_STAT=stoped"
) else (
set "CKI_SYSTEM_STAT=runing"
)
goto :eof
:ebis_case
rem label ebis_case
call :init_meta
set "CKI_SWITCH_CASE=false"
for %%i in (start stop status restart) do (
if %%i'==%CKI_OPTION_NAME%' SET "CKI_SWITCH_CASE=true"
)
if "%CKI_SWITCH_CASE%" == "false" call :message
if "%CKI_SWITCH_CASE%" == "true" call :case_%CKI_OPTION_NAME%
goto :eof
:case_start
rem label case_start
call :start
goto :eof
:case_stop
rem label case_stop
call :stop
goto :eof
:case_status
rem label case_status
call :status
goto :eof
:case_restart
rem label case_restart
call :restart
goto :eof
:start
rem label start
if "%CKI_SYSTEM_STAT%" == "runing" (
echo %CKI_DAEMON_NAME% already %CKI_SYSTEM_STAT%
)
if "%CKI_SYSTEM_STAT%" == "stoped" (
echo %CKI_DAEMON_NAME% is starting
)
goto :eof
:status
rem label status
echo %CKI_DAEMON_NAME% is %CKI_SYSTEM_STAT%
goto :eof
rem label stop
:stop
if "%CKI_SYSTEM_STAT%" == "stoped" (
echo %CKI_DAEMON_NAME% %CKI_SYSTEM_STAT%
)
if "%CKI_SYSTEM_STAT%" == "runing" (
echo %CKI_DAEMON_NAME% stoping
for /L %%a in (0,1,5) do (
call :init_meta
call :status
if %%a lss 5 (
if "%CKI_SYSTEM_STAT%" == "runing" (
timeout 3 > NUL
echo Wrong is %CKI_SYSTEM_STAT%
echo .
)
if "%CKI_SYSTEM_STAT%" == "stoped" (
echo stoped
goto :eof
)
)
if %%a equ 10 (
echo unable to stop
)
)
)
goto :eof
:restart
rem label restart
call :stop
call :start
goto :eof
rem label message
:message
echo Usage^: start^|stop^|^status^|restart
goto :eof
:freeze
rem label freeze
@pause
goto :eof
For your realization I simulated it by calling the status
label inside the loop to see the actual value of CKI_SYSTEM_STAT
also echo the value of CKI_SYSTEM_STAT
inside for
loop under stop
label. But CKI_SYSTEM_STAT
remain unchanged inside the for
loop. Here is the console output:
c:>batch\ebisd.bat stop
Random Number 8
Demon stoping
Random Number 2
Demon is stoped
Wrong is runing
.
Random Number 4
Demon is stoped
Wrong is runing
.
Random Number 8
Demon is runing
Wrong is runing
.
Random Number 9
Demon is runing
Wrong is runing
.
Random Number 4
Demon is stoped
Wrong is runing
.
Random Number 4
Demon is stoped
There might be mistake inside the script or unable to realize the scope of variable. It would be appreciable if anybody help us to find out the issue/bug of this script. Alternative solution highly expected.