Page 1 of 1
Check if process is running.
Posted: 04 Mar 2015 08:42
by (_osd_)
Hi guys
I have a problem: I need a bat that checks if a process (a .bat file) is already running.
Like this:
@echo off
[magic check if the process is running]
:a
cls
echo The process is not running.
pause
exit
:b
cls
echo The process is already running.
pause
Please help
thanks
Re: Check if process is running.
Posted: 04 Mar 2015 20:55
by foxidrive
Using tasklist is a way to determine if a task is running.
Re: Check if process is running.
Posted: 21 Mar 2015 04:12
by jwoegerbauer
A .BAT / .CMD file doesn't appear in TaskManager, but CMD.exe which processes the .BAT / .CMD file. So you have to convert your .BAT file into an .EXE. FYI: I use "AdvancedBatToExeConverter", which converts a .BAT / .CMD file into a 32-bit .EXE
To find out whether a specific .EXE is running, I have this in use:
Code: Select all
:checkForProcessExists
SET "_prog_exe=%~1"
SET proc_exists=0
IF "%_prog_exe%?" EQU "?" ( SET myErrorLevel=1 & GOTO :D1 )
CALL :getUniqueFileName "%unique_file_name%"
SET _tmp_file=%TEMP%\%unique_file_name%.txt
TASKLIST /FI "IMAGENAME eq %_prog_exe%" /FO CSV > %_tmp_file%
SET /p _proc_list=<%_tmp_file%
DEL /q %_tmp_file% > NUL 2>&1
FOR /f "skip=2 tokens=1 delims=," %%f IN (%_proc_list%) DO (
IF "%%f" EQU "%_prog_exe%" (
SET proc_exists=1
GOTO :D1
)
)
:D1
SET "_prog_exe=" & "SET _proc_list=" & SET "_tmp_file="
GOTO :EOF
Re: Check if process is running.
Posted: 21 Mar 2015 04:19
by ShadowThief
If the batch script has set a window title using the title command, you can find it in tasklist with
Code: Select all
tasklist /fi "windowtitle eq <batch script title>"
and just replace <batch script title> with whatever the window title is.
Re: Check if process is running.
Posted: 21 Mar 2015 04:32
by jwoegerbauer
ShadowThief wrote:If the batch script has set a window title using the title command, you can find it in tasklist with
Code: Select all
tasklist /fi "windowtitle eq <batch script title>"
and just replace <batch script title> with whatever the window title is.
Tested it: Doesn't work.
Code: Select all
TITLE "blahblah"
TASKLIST /FI "windowtitle eq blahblah" > %TEMP%\tasklist.txt
TYPE "%TEMP%\tasklist.txt"
PAUSE
DEL "%TEMP%\tasklist.txt"
Re: Check if process is running.
Posted: 21 Mar 2015 05:22
by ShadowThief
It won't work if you're checking to see if the verification script is running, but if you're checking to see if a second script is running (which is how I interpreted your question), then it absolutely
will work.
Re: Check if process is running.
Posted: 21 Mar 2015 08:32
by jwoegerbauer
ShadowThief wrote:It won't work if you're checking to see if the verification script is running, but if you're checking to see if a second script is running (which is how I interpreted your question), then it absolutely will work.
OK
If OP want to do the check within a batch file, at least I understood it so, not from console, as you have shown it, then this might help
Code: Select all
:checkForBatchIsRunning
IF "%~1?" EQU "?" GOTO :EOF
SET "_window_title=%~1"
TASKLIST /FI "Status eq Running" /FI "Windowtitle eq %_window_title%" /FO "TABLE" | FINDSTR /c:"PID" > NUL && ( SET "is_running=1" ) || ( SET "is_running=0" )
SET "_window_title="
GOTO :EOF
Of course, the batch file OP is testing on, must have a window title set with TITLE command, which must be passed to the sub
Code: Select all
CALL :checkForBatchIsRunning "%known_window_title%"
The complete code that OP wanted to get then would be
Code: Select all
@ECHO OFF
SET known_window_title=blahblah
rem [magic check if the process is running]
CALL :checkForBatchIsRunning "%known_window_title%"
CLS
IF "%is_running%" EQU "0" (
ECHO The process %known_window_title% is not running.
) ELSE (
ECHO The process %known_window_title% is already running.
)
PAUSE
EXIT /b
:checkForBatchIsRunning
IF "%~1?" EQU "?" GOTO :EOF
SET "_window_title=%~1"
TASKLIST /FI "Status eq Running" /FI "Windowtitle eq %_window_title%" /FO "TABLE" | FINDSTR /c:"PID" > NUL && ( SET "is_running=1" ) || ( SET "is_running=0" )
SET "_window_title="
GOTO :EOF
Re: Check if process is running.
Posted: 21 Mar 2015 19:34
by Squashman