windows script教學 run - 如何通過批處理腳本檢查進程是否正在運行




8 Answers

我使用grep另一個可能性是:

tasklist /FI "IMAGENAME eq myapp.exe" 2>NUL | find /I /N "myapp.exe">NUL
if "%ERRORLEVEL%"=="0" echo Program is running

它不需要保存一個額外的文件,所以我更喜歡這種方法。

set variable

我如何檢查應用程序是否從批處理(well cmd)文件運行?

如果程序已經運行,我不需要啟動另一個實例。 (我無法將應用程序更改為僅限單一實例。)

此外,該應用程序可以作為任何用戶運行。




我喜歡Chaosmaster的解決方案! 但是我尋找一個不啟動另一個外部程序的解決方案(如find.exefindstr.exe )。 所以我從Matt Lacey的解決方案中添加了這個想法,該解決方案創建了一個也是可以避免的臨時文件。 最後我可以找到一個相當簡單的解決方案,所以我分享它...

SETLOCAL EnableExtensions
set EXE=myprog.exe
FOR /F %%x IN ('tasklist /NH /FI "IMAGENAME eq %EXE%"') DO IF %%x == %EXE% goto FOUND
echo Not running
goto FIN
:FOUND
echo Running
:FIN

這對我很好用...




我使用安裝在Program Files \ PV中的http://www.teamcti.com/pview/prcview.htm中的PV.exe,使用這樣的批處理文件:

@echo off
PATH=%PATH%;%PROGRAMFILES%\PV;%PROGRAMFILES%\YourProgram
PV.EXE YourProgram.exe >nul
if ERRORLEVEL 1 goto Process_NotFound
:Process_Found
echo YourProgram is running
goto END
:Process_NotFound
echo YourProgram is not running
YourProgram.exe
goto END
:END



TrueY的答案似乎是最優雅的解決方案,但是,我不得不做一些混亂的事情,因為我不明白到底發生了什麼。 讓我清理一下,希望能為下一個人節省一些時間。

TrueY的修改答案:

::Change the name of notepad.exe to the process .exe that you're trying to track
::Process names are CASE SENSITIVE, so notepad.exe works but Notepad.exe does NOT
::Do not change IMAGENAME
::You can Copy and Paste this into an empty batch file and change the name of
::notepad.exe to the process you'd like to track
::Also, some large programs take a while to no longer show as not running, so
::give this batch a few seconds timer to avoid a false result!!

@echo off
SETLOCAL EnableExtensions

set EXE=notepad.exe

FOR /F %%x IN ('tasklist /NH /FI "IMAGENAME eq %EXE%"') DO IF %%x == %EXE% goto ProcessFound

goto ProcessNotFound

:ProcessFound

echo %EXE% is running
goto END
:ProcessNotFound
echo %EXE% is not running
goto END
:END
echo Finished!

無論如何,我希望這會有所幫助。 我知道有時閱讀批處理/命令行有時會讓人困惑,如果你像我一樣是新手。




我喜歡WMICTASKLIST工具,但它們不適用於家庭/基本版本的Windows。 QPROCESS一種方法是在幾乎所有的Windows機器上使用QPROCESS命令(對於那些有終端服務的人 - 我認為只有在沒有SP2的情況下才能贏得XP,所以實際上每個Windows機器):

@echo off
:check_process
setlocal
if "%~1" equ "" echo pass the process name as forst argument && exit /b 1
:: first argument is the process you want to check if running
set process_to_check=%~1
:: QPROCESS can display only the first 12 symbols of the running process
:: If other tool is used the line bellow could be deleted
set process_to_check=%process_to_check:~0,12%

QPROCESS * | find /i "%process_to_check%" >nul 2>&1 && (
    echo process %process_to_check%  is running
) || (
    echo process %process_to_check%  is not running
)
endlocal

QPROCESS命令不如TASKLIST強大,僅限於顯示進程名稱的12個符號,但如果TASKLIST不可用,應予以考慮。

更簡單的用法,如果進程作為參數使用名稱(在這種情況下傳遞可執行文件名稱時, .exe後綴是強制性的):

@echo off
:check_process
setlocal
if "%~1" equ "" echo pass the process name as forst argument && exit /b 1
:: first argument is the process you want to check if running
:: .exe suffix is mandatory
set "process_to_check=%~1"


QPROCESS "%process_to_check%" >nul 2>&1 && (
    echo process %process_to_check%  is running
) || (
    echo process %process_to_check%  is not running
)
endlocal

QPROCESS使用的兩種方法之間的區別在於QPROCESS *將列出所有進程,而QPROCESS some.exe將僅過濾當前用戶的進程。

通過Windows腳本使用WMI對象而不是WMIC也是一個選項。它也應該在每台Windows機器上運行(不包括那些WSH關閉的情況,但這種情況很少見)。這裡的bat文件列出了所有進程通過WMI類,可以在上面的腳本中使用,而不是QPROCESS (它是一個jscript / bat混合體,應該保存為.bat ):

@if (@X)==(@Y) @end /* JSCRIPT COMMENT **


@echo off
cscript //E:JScript //nologo "%~f0"
exit /b

************** end of JSCRIPT COMMENT **/


var winmgmts = GetObject("winmgmts:\\\\.\\root\\cimv2");
var colProcess = winmgmts.ExecQuery("Select * from Win32_Process");
var processes =  new Enumerator(colProcess);
for (;!processes.atEnd();processes.moveNext()) {
    var process=processes.item();
    WScript.Echo( process.processID + "   " + process.Name );
}

還有一個將檢查進程是否正在運行的修改:

@if (@X)==(@Y) @end /* JSCRIPT COMMENT **


@echo off
if "%~1" equ "" echo pass the process name as forst argument && exit /b 1
:: first argument is the process you want to check if running
set process_to_check=%~1

cscript //E:JScript //nologo "%~f0" | find /i "%process_to_check%" >nul 2>&1 && (
    echo process %process_to_check%  is running
) || (
    echo process %process_to_check%  is not running
)

exit /b

************** end of JSCRIPT COMMENT **/


var winmgmts = GetObject("winmgmts:\\\\.\\root\\cimv2");
var colProcess = winmgmts.ExecQuery("Select * from Win32_Process");
var processes =  new Enumerator(colProcess);
for (;!processes.atEnd();processes.moveNext()) {
    var process=processes.item();
    WScript.Echo( process.processID + "   " + process.Name );
}

這兩個選項可以用在沒有TASKLIST機器上。

最終的技術是使用MSHTA 。 這將在XP及以上版本的每台Windows機器上運行,並且不依賴於Windows腳本主機設置。 儘管MSHTA的調用可能會降低一點性能(也應該保存為蝙蝠):

@if (@X)==(@Y) @end /* JSCRIPT COMMENT **
@echo off

setlocal
if "%~1" equ "" echo pass the process name as forst argument && exit /b 1
:: first argument is the process you want to check if running

set process_to_check=%~1


mshta "about:<script language='javascript' src='file://%~dpnxf0'></script>" | find /i "%process_to_check%" >nul 2>&1 && (
    echo process %process_to_check%  is running
) || (
    echo process %process_to_check%  is not running
)
endlocal
exit /b
************** end of JSCRIPT COMMENT **/


   var fso= new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1);


   var winmgmts = GetObject("winmgmts:\\\\.\\root\\cimv2");
   var colProcess = winmgmts.ExecQuery("Select * from Win32_Process");
   var processes =  new Enumerator(colProcess);
   for (;!processes.atEnd();processes.moveNext()) {
    var process=processes.item();
    fso.Write( process.processID + "   " + process.Name + "\n");
   }
   close();



TASKLIST | FINDSTR ProgramName || START "" "Path\ProgramName.exe"



我使用了Matt提供腳本 (2008-10-02)。 我唯一遇到的問題是它不會刪除search.log文件。 我期望,因為我不得不cd到另一個位置開始我的程序。 我可以回到BAT文件和search.log位置,但它仍然不會刪除。 所以我解決了這個問題,首先刪除了search.log文件,而不是最後一個。

del search.log

tasklist /FI "IMAGENAME eq myprog.exe" /FO CSV > search.log

FOR /F %%A IN (search.log) DO IF %%-zA EQU 0 GOTO end

cd "C:\Program Files\MyLoc\bin"

myprog.exe myuser mypwd

:end



我通常在cmd提示符下執行以下命令來檢查我的program.exe是否正在運行:

tasklist | grep program



Related