Check if a certain script/process is already running?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
pstein
Posts: 125
Joined: 09 Nov 2011 01:42

Check if a certain script/process is already running?

#1 Post by pstein » 02 May 2013 01:59

From a DOS batch script (under 64bit Win7) I want to check
if there is already a script with the same name running.

Lets say the name of the script is myuniquescript.bat

So this running script can be identified in TaskManager by process name =cmd.exe and
the cmdline

cmd.exe /c "D:\tools\scripts\myuniquescript.bat"

How can I do this from within a DOS batch script (under 64bit Win7)

Peter

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Check if a certain script/process is already running?

#2 Post by abc0502 » 02 May 2013 02:50

You can kill running programs by process names, but as cmd runs all the batch file, if you kill it you kill all batch file running.
This is a mix of vbscript and batch file, set the window title you want to kill and run this batch.
Don't forget to leave empty line at the end of your batch file

Code: Select all

@Echo OFF
SET "WindowTitle=Test Title"
(
FOR /F "tokens=1*" %%a IN ('findstr "^:VBS: " ^< "%~F0"') DO Echo.%%b
)>"%temp%\KillWindow.vbs"
Cscript //nologo "%temp%\KillWindow.vbs" "%WindowTitle%"
Exit
:KillWindow
:VBS: Option Explicit
:VBS: Dim arrProcesses, arrWindowTitles
:VBS: Dim i, intProcesses
:VBS: Dim objJSSys
:VBS: Dim strProcList
:VBS: Set objJSSys = CreateObject( "JSSys3.ops" )
:VBS: objJSSys.CloseProgram chr(34) & WScript.Arguments(0) & chr(34), 1
:VBS: Set objJSSys = Nothing

REM MUST LEAVE EMPTY LINE AFTER THIS ONE



Require JSSys.dll
Source VBscript : Link

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

Re: Check if a certain script/process is already running?

#3 Post by foxidrive » 02 May 2013 03:01

If you just want to find if a bat is running, and you can set the Title command to test123 then this will tell you if it is running.

Code: Select all

@echo off
tasklist /fi "IMAGENAME eq cmd.exe" /fi "WINDOWTITLE eq test123" |find "No tasks" >nul || echo window was found
pause


From there you can kill the task with taskkill or do whatever you need.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Check if a certain script/process is already running?

#4 Post by abc0502 » 02 May 2013 03:06

:oops: I have no idea why i thought it was about killing the running batch :roll:

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

Re: Check if a certain script/process is already running?

#5 Post by foxidrive » 02 May 2013 03:14

abc0502 wrote::oops: I have no idea why i thought it was about killing the running batch :roll:



Mistaking the question for something else happens more and more often the older you get, too. :D

Or missing out an important piece of info in a question. :D

I do it every so often, you aren't alone...

pstein
Posts: 125
Joined: 09 Nov 2011 01:42

Re: Check if a certain script/process is already running?

#6 Post by pstein » 04 May 2013 00:28

foxidrive wrote:If you just want to find if a bat is running, and you can set the Title command to test123 then this will tell you if it is running.

Code: Select all

@echo off
tasklist /fi "WINDOWTITLE eq test123" >nul && echo window was found
pause



Sorry, this will ALWAYS show "window was found"! Even before I set the window title.

So is there a better way of detection?

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

Re: Check if a certain script/process is already running?

#7 Post by foxidrive » 04 May 2013 07:08

Sorry, I didn't test it for a case when the window was closed so didn't see the problem. Try this: (same as the above post, which I edited)

Code: Select all

@echo off
tasklist /fi "IMAGENAME eq cmd.exe" /fi "WINDOWTITLE eq test123" |find "No tasks" >nul || echo window was found
pause

pstein
Posts: 125
Joined: 09 Nov 2011 01:42

Re: Check if a certain script/process is already running?

#8 Post by pstein » 15 May 2013 00:05

foxidrive wrote:

Code: Select all

@echo off
tasklist /fi "IMAGENAME eq cmd.exe" /fi "WINDOWTITLE eq test123" |find "No tasks" >nul || echo window was found
pause


Ok, thank you.
But there is still a problem. I simplified the situation in my previous description.
The title is NOT just "test123" but consists of a longer text string.

When the title is for example (mind the TWO blanks after the colon!):

Administrator: test123.bat

then

/fi "WINDOWTITLE eq test123"

does NOT find the process. because the filter requires the FULL string and does not mean "contains (as sub-string)".

How can I solve the problem?

How can handle the blanks inside? When I quote the window title then a quote miss interpretation takes place:

/f "WINDOWTITLE eq "Administrator: test123.bat""

However I would prefer a "contains" filter workaround.

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

Re: Check if a certain script/process is already running?

#9 Post by foxidrive » 15 May 2013 04:34

This works in the command here in Win8 just fine.

Code: Select all

tasklist /fi "IMAGENAME eq cmd.exe" /fi "WINDOWTITLE eq Admin: test123" 

Post Reply