Check if process is running.
Moderator: DosItHelp
Check if process is running.
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
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.
Using tasklist is a way to determine if a task is running.
-
- Posts: 33
- Joined: 01 Jan 2013 12:09
Re: Check if process is running.
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:
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
Last edited by jwoegerbauer on 21 Mar 2015 04:24, edited 1 time in total.
-
- Expert
- Posts: 1166
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
Re: Check if process is running.
If the batch script has set a window title using the title command, you can find it in tasklist with
and just replace <batch script title> with whatever the window title is.
Code: Select all
tasklist /fi "windowtitle eq <batch script title>"
and just replace <batch script title> with whatever the window title is.
-
- Posts: 33
- Joined: 01 Jan 2013 12:09
Re: Check if process is running.
ShadowThief wrote:If the batch script has set a window title using the title command, you can find it in tasklist withCode: 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"
-
- Expert
- Posts: 1166
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
Re: Check if process is running.
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.
-
- Posts: 33
- Joined: 01 Jan 2013 12:09
Re: Check if process is running.
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