我们需要使用批处理脚本来控制像start
, stop
,这样的守护进程restart
,其中变量是可变的,根据守护进程的条件动态更改。这里我们要根据问题来模拟你的实现。随机更改,但在循环内它在标签下保持不变。status
CKI_SYSTEM_STAT
CKI_SYSTEM_STAT
for
stop
由于这个原因,当执行停止操作时,我们无法检测到守护程序是否成功停止。这是供您考虑的批处理脚本。
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
为了您的实现,我通过调用循环内的标签来模拟它,以查看标签下的内部循环值status
的实际值。但在循环内保持不变。这是控制台输出:CKI_SYSTEM_STAT
CKI_SYSTEM_STAT
for
stop
CKI_SYSTEM_STAT
for
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
脚本内部可能有错误或无法实现变量的范围。如果有人帮助我们找出这个脚本的问题/错误,那将是非常重要的。备受期待的替代解决方案。