簡體   English   中英

如何檢查批處理文件是否運行並避免雙重執行?

[英]How to check if a batch file runs and avoid a dual execute?

我想制作一個批處理腳本,但我想避免雙重執行。 所以我試圖檢查它是否正在運行。

我使用了以下方法,但我ERROR: The process "MyBatchTool" not foundERROR: The process "MyBatchTool" not found消息ERROR: The process "MyBatchTool" not found

echo off
TITLE MyBatchTool
SETLOCAL ENABLEDELAYEDEXPANSION
tasklist /fi "imagename eq MyBatchTool" |find ":" > nul
if errorlevel 1 echo "Is Running"
pause

你有什么建議嗎 ?

echo off
TITLE MyBatchTool
SETLOCAL ENABLEDELAYEDEXPANSION
tasklist /fi "imagename eq MyBatchTool" 2>nul |find ":" > nul 2>nul && (
   echo "Is not Running"
   color
)||(
    echo "Is running"

)
pause

您可以使用文件鎖,即使出現錯誤,批處理文件退出時,該鎖也會被刪除。
而且只有一個進程可以持有該鎖,這是安全的。

2> nul (
    9> lock.tmp 2>&1 (
    call :main
    ) || ( echo Is still runiing )
)
exit /b

:main
echo This will only run in one instance
ping -n 10 localhost

第一部分是用於創建排他鎖文件。 當您嘗試再次運行批處理時, 2>nul阻止錯誤消息。

有關更多示例和描述,您可以在SO或dostips.com上搜索dbenham關於該主題的帖子。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM