2014-12-31 61 views
0

我有一個windows批處理文件,它調用另一個文件Services,它具有需要重新啓動的服務和機器的列表,但其中一個服務不需要啓動週末。有誰知道我可以如何在服務文件中包含%dow%函數然後調用基於dow的服務? PS - 它調用另一個批處理文件STOPANDWAIT,它確保服務尚未運行。在/ F語句的批處理文件中加入dow

這裏是我的代碼:

:start_services 
set SECTION=start_services 
REM We will try to start all services 
echo. 
echo  ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» 
echo     Starting services        
echo  ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ 
%TIME_STAMP% BEFORE Starting services >> %TIMETRAC_FILE% 
REM Make sure the file that contains the services to be stopped and started actually exists 
if not exist %SERVICES_FILE% (
    %TIME_STAMP% Services file not found. >> %TIMETRAC_FILE% 
    echo Services file not found. 
    goto ERROR_FOUND 
) 

for /F "tokens=1,2* delims=," %%i in (%SERVICES%) do (
    %TIME_STAMP% Starting %%i on %%j >> %TIMETRAC_FILE% 
    @echo Calling StartServiceAndWait for %%i on %%j 
    call StartServiceAndWait %%j %%i 
    %TIME_STAMP% Starting %%i Done >> %TIMETRAC_FILE% 
) 


%TIME_STAMP% AFTER Starting services >> %TIMETRAC_FILE% 
if "%END_TAG%" == "start_services" goto PARTIAL_SUCCESS 

我SERVICES%%文件看起來像這樣:

"EMSXComplianceService",\\bosxiptest 
"Mxs.Xom",\\bosxiptest 
"Mfs.Xom.Emsx",\\bosxiptest 
"Mxs.Xom",\\bosxiptest 
"Mxs.Xns",\\bosxiptest 
"Mfs Wrangler Messanger",\\bosxiptest 

,我確實有星期三設置爲

日等全球%的Dow%

預先感謝您!

謝謝!

回答

0

你沒有說出你的%dow%是什麼樣的,你的%services%文件樣本,所以讓我們開始猜測。

嘗試修改主循環在你的程序中,以這樣的:

SET "runtoday=" 
for /F "tokens=1,2* delims=," %%i in (%SERVICES%) do IF "%%j%%k"=="" (
if defined runtoday (
    %TIME_STAMP% Starting %%i on %%j >> %TIMETRAC_FILE% 
    @echo Calling StartServiceAndWait for %%i on %%j 
    call StartServiceAndWait %%j %%i 
    %TIME_STAMP% Starting %%i Done >> %TIMETRAC_FILE% 
) 
) ELSE (
echo %%i|findstr "%dow%">nul 
if errorlevel 1 (set "runtoday=") else (set "runtoday=Y") 
) 

然後在您%SERVICES%文件,

[Mon] 
service1,runs,Monday 
service2,runs,Monday 
[Tue Fri] 
service3,runs,Tuesday and Friday 
service4,runs,Tuesday and Friday 

,你DayNames中/數字/不管是上,做了線不是包含逗號。按照.INI文件中的樣式,我將它們放在括號中以使它們變得明顯。

注:如果您在文件(上升到第[DAYNAME]線)的起點設置runtoday最初是空的,過程將運行。如果您最初將其設置爲非空,則每行將運行,直到遇到[dayname]行,並開啓審查。

+0

喜@Magoo - 非常感謝你的幫助。當我運行這一點,我的批處理說ELSE不被視爲一個有效的命令,我失敗了。我的服務文件是非常簡單的,現在看起來是這樣的:週一週二週三週四週五] 「EMSXComplianceService」,\\ bosxiptest **哪裏有服務,服務器**這種做法是非常接近我所期待的,如果我可以調整它,我會做。再次感謝你的幫助! –

0

你可以使用'SET MYDATE =%DATE:〜0.3%'來得到當前的DOW。

假設你的服務文件是類似於:

ServiceName1,Machine1 
ServiceName2,Machine2 

更改線路:

ServiceName1,Machine1,1,1,1,1,1,1,1 
ServiceName2,Machine2,0,1,1,1,1,1,0 

你的循環會是這個樣子:

SET MYDATE=%DATE:~0,3% 

for /F "tokens=1,2,3,4,5,6,7,8,9 delims=," %%i in (C:\Temp\ServiceList.txt) do (
    ECHO Checking %%i on %%j 

IF %MYDATE% == Mon (IF %%k == 1 (
%TIME_STAMP% Starting %%i on %%j >> %TIMETRAC_FILE% 
@echo Calling StartServiceAndWait for %%i on %%j 
call StartServiceAndWait %%j %%i 
%TIME_STAMP% Starting %%i Done >> %TIMETRAC_FILE% 
)) 

IF %MYDATE% == Tue (IF %%l == 1 (
%TIME_STAMP% Starting %%i on %%j >> %TIMETRAC_FILE% 
@echo Calling StartServiceAndWait for %%i on %%j 
call StartServiceAndWait %%j %%i 
%TIME_STAMP% Starting %%i Done >> %TIMETRAC_FILE% 
)) 

IF %MYDATE% == Wed (IF %%m == 1 (
%TIME_STAMP% Starting %%i on %%j >> %TIMETRAC_FILE% 
@echo Calling StartServiceAndWait for %%i on %%j 
call StartServiceAndWait %%j %%i 
%TIME_STAMP% Starting %%i Done >> %TIMETRAC_FILE% 
)) 

IF %MYDATE% == Thu (IF %%n == 1 (
%TIME_STAMP% Starting %%i on %%j >> %TIMETRAC_FILE% 
@echo Calling StartServiceAndWait for %%i on %%j 
call StartServiceAndWait %%j %%i 
%TIME_STAMP% Starting %%i Done >> %TIMETRAC_FILE% 
)) 

IF %MYDATE% == Fri (IF %%o == 1 (
%TIME_STAMP% Starting %%i on %%j >> %TIMETRAC_FILE% 
@echo Calling StartServiceAndWait for %%i on %%j 
call StartServiceAndWait %%j %%i 
%TIME_STAMP% Starting %%i Done >> %TIMETRAC_FILE% 
)) 

IF %MYDATE% == Sat (IF %%p == 1 (
%TIME_STAMP% Starting %%i on %%j >> %TIMETRAC_FILE% 
@echo Calling StartServiceAndWait for %%i on %%j 
call StartServiceAndWait %%j %%i 
%TIME_STAMP% Starting %%i Done >> %TIMETRAC_FILE% 
)) 

IF %MYDATE% == Sun (IF %%q == 1 (
%TIME_STAMP% Starting %%i on %%j >> %TIMETRAC_FILE% 
@echo Calling StartServiceAndWait for %%i on %%j 
call StartServiceAndWait %%j %%i 
%TIME_STAMP% Starting %%i Done >> %TIMETRAC_FILE% 
)) 

) 

我個人把

(
%TIME_STAMP% Starting %%i on %%j >> %TIMETRAC_FILE% 
@echo Calling StartServiceAndWait for %%i on %%j 
call StartServiceAndWait %%j %%i 
%TIME_STAMP% Starting %%i Done >> %TIMETRAC_FILE% 
) 

到一個單獨的批處理文件,並調用它,所以它不會在批處理文件中重複七次。

有可能是一種更乾淨的方式來做到這一點,但這是你在NYE上發佈問題的方法。:)

0

如果您%date%變量嵌入了一天(這取決於區域用戶設置),那麼試試這個:

set "skip=" 
echo %date%|find /i "sat" >nul && set skip=1 
echo %date%|find /i "sun" >nul && set skip=1 
if not defined skip (call "your batch file to change services.bat") else (goto :weekend)