How can I stop one instance via cmd of a process when several are running? - windows

I start a program from a scilab script via the command line, start myprog.exe.
After the start my scilab script needs to keep going.
Now I want to stop exactly this instance of the process via the command line too, even if several instances of the same program are running.
Is that possible?
I know how to query via batch files whether a process of this program is running and then stop it, but I don't know how to get the exact allocation.
Is there something like a process id?
I use this to check if the process is running:
tasklist /fi "imagename eq ccx.exe" |find ":" > nul
if errorlevel 1 echo Program is running
if not errorlevel 1 echo Program is not running

Use the command tasklist to view all running tasks with their PID
then
Taskkill /PID 26356 /F
or
Taskkill /IM myprog.exe /F

Related

how to kill a cmd process without killing others cmds?

I'm working on a batch file that is supposed to START a process (CMD) and then it should kill the process after finished. Problem is, that Imagename is cmd.exe and the other problem is that it should be running on Jenkins.
This is what I have tested:
Getting PID with wmic using name of window to find process -> Failed at Jenkins
Taskkill by naming the window-> Failed because Jenkins does not
display windows due to security issues.
Taskkill by imagename -> Failed because there are other cmd processes
running at the same time
Taskkill with pid but pid from the last cmd started. -Works but it is
not very safe.
I couldn´t understand how wmic works but as I see, I cannot start a process with a command like with START command.
Conditions:
It can't be kill after some time because I need the output from the
mergetool and sometimes mergetool can take too long.
It should run at same time with other (cmd) processes // Jenkins
My question, is there a way of getting the PID from the START Command?
Here are some questions that helped me a lot!
Windows batch file : PID of last process?
Compare number of a specific process to a number
CODE:
set "console_name=cmd.exe"
set "git_command=%gitcmd% mergetool ^>output.txt"
tasklist /FI "imagename eq %console_name%" /NH /FO csv > task-before.txt
START "mergetool_w" CMD /c %git_command%
tasklist /FI "imagename eq %console_name%" /NH /FO csv > task-after.txt
for /f "delims=, tokens=2,*" %%A in ('fc /L /LB1 task-before.txt task-after.txt') do set pid=%%A
pid=!pid:"=!
echo pid is %pid%
TASKKILL /t /pid %pid% /f
You could actually use findstr for checking what tasks have been added after your start command line, relying on your files task-before.txt and task-after.txt:
findstr /LXVG:task-before.txt task-after.txt
Due to a nasty bug, this might under some circumstances lead to an unexpected output. To prevent that, add the /I option, if you can live with case-insensitive searches:
findstr /ILXVG:task-before.txt task-after.txt
Yes it's very possible. I'm going to take code from my previous awnser on another post here: Stop Execution of Batch File after 20 Seconds and move to Next
I want to first assume "mergetool_w" is the name of the CMD you are opining with the start...
The way you want to go about this is to search the tasklist for your console title and extract the PID# out of the context. The find suffix can be used to "Filter" the results along with tokens=2 to extract only the PID#.
FOR /F "tokens=2" %%# in ('tasklist /v ^| find "mergetool_w"') do set PID=%%#
From there, you can now kill this new window using the taskkill /pid command. The PID# is stored in the string %PID% so the command is simple:
taskkill /pid %PID% /t /f
Finaly, it looks as if you are trying to "Log" the data so feel free to put > text-task.txt where it's needed.

TASKKILL specific Python script

I have a working TASKKILL command that kills python.exe using Process Name
I'd like to narrow the scope of the command to kill a specific process (myScript.py) but can't use ProcessID as it changes with every run.
Is there a way I can add detail from the Command Line which knows the python script's name?
Current Command:
Taskkill /IM python.exe /F >nul 2>&1
if errorlevel 1 (echo PYTHON.exe NOT FOUND) else (echo PYTHON.exe KILLED)
You might be able to kill it based on your Python script's memory usage. In my case, the Python script is running a GUI, so the size gives it away.
taskkill /f /fi "IMAGENAME eq python.exe" /fi "MEMUSAGE gt 130000"
This reads as, forcefully kill the task (taskkill /f) identified by (/fi) the Python executable (IMAGENAME eq python.exe) which is using more than 130,000KB (MEMUSAGE gt 130000)1.
1 See taskkill /? for builtin help.
N.B. You might find this SO post helpful: Find Windows PID of a python script with Windows Command Prompt. Unfortunately, for me, It Doesn't Work™ but maybe you will have better luck.

Re-opening files in Batch

For definitely not malicious reasons, I need to have a batch file always open.
I have some base code:
:b
echo off
tasklist /fi "imagename eq cmd.exe" |find ":" > nul
if errorlevel 1 taskkill /f /im "game.bat"&start game.bat
goto b
And it works fine if want notepad.exe or blah.txt and etc.
Except for batch files, as the program itself is a batch file,
the system sees cmd.exe is already open.
It works except for batch files, as the system sees cmd.exe is already open.
Give your batch file a Title by adding the following command to game.bat:
title %~nx0
Check if game.bat is running by using tasklist with /v option:
:b
#echo off
tasklist /v | find "game.bat" > nul
rem errorlevel 1 means game.bat is not running, so start it
if errorlevel 1 start game.bat
rem timeout to avoid excessive processor load
timeout 60
goto b
Further Reading
An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
tasklist - TaskList displays all running applications and services with their Process ID (PID) This can be run on either a local or a remote computer.
timeout - Delay execution for a few seconds or minutes, for use within a batch file.
title - Change the title displayed above the CMD window.

Wait for a .bat file to close within a windows batch file

I need to create a windows batch file (*.bat) file that only runs its commands if certain processes (and batch files) are NOT running.
I have looked at a solution that works for processes (*.exe) here:
How to wait for a process to terminate to execute another process in batch file
I want to do something very similar, however, there is one difficulty: Batch files show up as "cmd.exe" in the "TASKLIST" command.
I want to check if a specific bat file is running, for example: "C:\mybatch.bat", and if it is, wait until it is closed.
Checking if a specific bat file mybatch.bat is running could be a tougher task than it could look at first sight.
Looking for a particular window title in tasklist /V as well as testing CommandLine property in wmic process where "name='cmd.exe'" get CommandLine might fail under some imaginable circumstance.
1st. Can you
add title ThisIsDistinguishingString command at beginning of the mybatch.bat and
remove all other title commands from mybatch.bat and
ensure that mybatch.bat does not call another batch script(s) containing a title command?
Then check errorlevel returned from find command as follows:
:testMybatch
tasklist /V /FI "imagename eq cmd.exe" | find "ThisIsDistinguishingString" > nul
if errorlevel 1 (
rem echo mybatch.bat batch not found
) else (
echo mybatch.bat is running %date% %time%
timeout /T 10 /NOBREAK >NUL 2>&1
goto :testMybatch
)
2nd. Otherwise, check if wmic Windows Management Instrumentation command output could help
wmic process where "name='cmd.exe'" get /value
Then you could detect mybatch.bat in its output narrowed to
wmic process where "name='cmd.exe'" get CommandLine, ProcessID
Note that wmic could return some Win32_Process class properties, particularly CommandLine, empty if a particular process was launched under another user account or elevated (run as administrator).
Elevated wmic returns all properties in full.
What you say happens by default.
To test, crate a new .bat file (let's say 1.bat) and put in it
calc
mspaint
Save and run it.
Calculator will start. You will notice that Paitbrush will launch only when you have closed calculator.

Batch script to kill a process if not responding in a for loop

I am trying to install some windows standalone update files and to do this I need to use wusu.exe. Every now and again wusu.exe will hang. I have created a batch file called prereqs.bat and in this file I have calls to the wusu.exe. I need the code to kill wusu.exe if it hangs, and retry it again.
This is my code as it stands now:
:PreReqs32
taskkill /im prereqs32.bat /f
taskkill /im wusa.exe /f
when it loops back, it kills both the batch file and wusa.exe
start cmd /k c:\windows\temp\prereqs.bat
An outside process so that I can kill wusu.exe if things go awry.
timeout /t 240 /NOBREAK
this timeout is to wait until the install is complete which is sometimes not enough.
taskkill /im "[wusa.exe]" /fi "STATUS eq NOT RESPONDING"
if "%ERRORLEVEL%"=="1" goto PreReqs32
Is there a way for some sort of FOR loop, with logic to exit if the status is not "not responding"?
Also, as a bonus, is there a way to forgo the timeout and just "wait" for prereqs.bat to be complete before moving on, assuming that wusu.exe has not hung?

Resources