Check if process is running.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
(_osd_)
Posts: 25
Joined: 01 Mar 2015 07:41

Check if process is running.

#1 Post by (_osd_) » 04 Mar 2015 08:42

Hi guys :D
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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Check if process is running.

#2 Post by foxidrive » 04 Mar 2015 20:55

Using tasklist is a way to determine if a task is running.

jwoegerbauer
Posts: 33
Joined: 01 Jan 2013 12:09

Re: Check if process is running.

#3 Post by jwoegerbauer » 21 Mar 2015 04:12

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
Last edited by jwoegerbauer on 21 Mar 2015 04:24, edited 1 time in total.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Check if process is running.

#4 Post by ShadowThief » 21 Mar 2015 04:19

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.

jwoegerbauer
Posts: 33
Joined: 01 Jan 2013 12:09

Re: Check if process is running.

#5 Post by jwoegerbauer » 21 Mar 2015 04:32

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"

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Check if process is running.

#6 Post by ShadowThief » 21 Mar 2015 05:22

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.

Image

jwoegerbauer
Posts: 33
Joined: 01 Jan 2013 12:09

Re: Check if process is running.

#7 Post by jwoegerbauer » 21 Mar 2015 08:32

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

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Check if process is running.

#8 Post by Squashman » 21 Mar 2015 19:34


Post Reply