Page 1 of 1
Check if a certain script/process is already running?
Posted: 02 May 2013 01:59
by pstein
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
Re: Check if a certain script/process is already running?
Posted: 02 May 2013 02:50
by abc0502
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 fileCode: 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.dllSource VBscript : Link
Re: Check if a certain script/process is already running?
Posted: 02 May 2013 03:01
by foxidrive
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.
Re: Check if a certain script/process is already running?
Posted: 02 May 2013 03:06
by abc0502
I have no idea why i thought it was about killing the running batch
Re: Check if a certain script/process is already running?
Posted: 02 May 2013 03:14
by foxidrive
abc0502 wrote::oops: I have no idea why i thought it was about killing the running batch
Mistaking the question for something else happens more and more often the older you get, too.
Or missing out an important piece of info in a question.
I do it every so often, you aren't alone...
Re: Check if a certain script/process is already running?
Posted: 04 May 2013 00:28
by pstein
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?
Re: Check if a certain script/process is already running?
Posted: 04 May 2013 07:08
by foxidrive
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
Re: Check if a certain script/process is already running?
Posted: 15 May 2013 00:05
by pstein
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.
Re: Check if a certain script/process is already running?
Posted: 15 May 2013 04:34
by foxidrive
This works in the command here in Win8 just fine.
Code: Select all
tasklist /fi "IMAGENAME eq cmd.exe" /fi "WINDOWTITLE eq Admin: test123"