I have this sitting in a batch file, and I'm wondering why it's throwing
"The system cannot find the file TASKLIST"
FOR /F "usebackq tokens=2 skip=2" %%i IN (TASKLIST /FI "IMAGENAME eq explorer.exe") DO taskkill /F /PID %%i
More importantly, the problem is that that command - killing explorer.exe - has to run in the cmd spawned by the command
at xx:xx /interactive "cmd.exe"
How would I pipe that taskkill command into the new command prompt that would be spawned by the at command?
Thank you for your time.
if you want to process a exit of a command use single qutoes or back quotes with "backq" parameter:
FOR /F "tokens=2 skip=2" %%i IN ('TASKLIST /FI ^"IMAGENAME eq explorer.exe^"') DO taskkill /F /PID %%i
(im not sure if your backquotes are displayed because they meta-symbol for code block in stackoverflow)
Taskkill also can kill a process by image name
taskkill /im explorer* /f
Related
I am trying to kill specific services using the PID from "SC QUERYEX wuauserv". But I don't know how to pull the PID shown in the results to then run "taskkill /pid /f [PID]". I am trying to make a batch file which I can use on multiple machines remotely.
I have tried a couple of suggestions made in other similar questions found on google, but wuauserv is not being killed for some reason.
# echo off
cmd /c FOR /F "usebackq tokens=2 skip=3" %%i IN (tasklist /fi "services eq wuauserv") DO taskkill /PID %%
pause
The above is what I have, but it's not finding the specific service in the task list. Can anyone assist?
Essentially, because you specified usebackq you need to put your Command within backticks ie: IN (`Command`) DO () you also need to include the variable letter you specified earlier in the ending section which you have, right now it is just %% given you set up i as the variable letter it should be %%i.
Also the CMD /C portion is just not needed at all.
Thats said, just drop the UseBackQ it isn't necessary, here is yoru code cleaned up a little.
#(
SETLOCAL EnableDelayedExpansion
echo off
)
FOR /F "Tokens=2" %%I IN ('
Tasklist /fi "Services eq wuauserv"
^| FIND /I "wuauserv"
') DO (
ECHO Killing PID %%I
Taskkill /PID %%I
)
PAUSE
I am unable to call a bat process within another bat.
This is the situation.
I have a n1.bat that basically contains:
TITLE "n1.bat"
...
...
start /b /MIN "n2.bat"
...
The n2.bat contains:
TITLE "n2.bat"
...
...
start someother.bat
start another.bat
exit
Now I use a third bat (n3.bat) that must kill everything.
TITLE "n3.bat"
...
TASKKILL /F /T /PID n1_PID
...
exit 0
Unfortunately when n3.bat ends a window called n1.bat - n2.bat remains active (the echo output belongs to n2.bat).
I tried to kill it in some ways:
1- Tried to get the process pid from tasklist -> there is no process called n2.bat or similar
TASKLIST /V /NH>Tasks.txt
FOR /F "tokens=2 delims= " %%n2_pid IN ('FINDSTR n2.bat Tasks.txt') DO SET PID=%%n2_pid
TASKKILL /PID %n2_pid%
2- Trying to use TASKKILL /F /FI "WINDOWTITLE eq n2.bat" -> no process found
I tried the previous solutions also starting n2.bat with
start /min "n2.bat" (so without /b)
with no success.
The only way I manage to kill it is to calling
TASKKILL /F /IM cmd.exe
that I really would like to avoid since it obviously kills all opened cmd.
Any ideas to retrieve the guilty pid?
I'd like to create the below two separate lines of cmd script into a bat file. The PID changes so I'm having a hard time figuring out how to set this service PID to be dynamic.
Any help would be welcomed thank you!!
sc queryex DWDesktopService
taskkill /f /pid ####
You just need to scrape the PID value out of the sc output. It is awkward in cmd scripting. Change the service name to yours. When you are satisfied that the correct PID will be killed, remove the echo from the TASKKILL line.
FOR /F "usebackq delims=: tokens=1,2" %%a IN (`sc queryex "BITS" ^| FIND /I " PID "`) DO (
SET "PID=%%b"
)
echo TASKKILL /F /PID %PID%
I am trying to get windows processes matching some certain criteria, e.g. they are like "123456.exe" and trying to kill them with tasklist. I am trying to do it like that:
FOR /F "usebackq tokens=2 skip=2" %i IN (`tasklist |findstr /r "[0-9].exe") DO taskkill /PID %i
which is not right and I don't know why.... Can anyone give me a hint?
Thanx in advance!
FOR /F "usebackq tokens=2" %i IN (`tasklist ^| findstr /r /b "[0-9][0-9]*[.]exe"`) DO taskkill /pid %i
Several changes:
The command_to_process needs back quotes (``) on both sides of the command.
Pipes ("|") inside of the command_to_process need to be escaped with a caret ("^").
Your findstr command would match all processes that have a digit before the ".exe". For example, "myapp4.exe" would also have been killed. The version I provide will match process names solely containing numbers.
The "skip=2" option would skip the first two lines output from findstr, not tasklist. Since the regular expression won't match anything in the first two lines output from tasklist, you're safe to remove the skip option.
By the way, if you place this command in a batch script, remember to use "%%i" instead of "%i" for your parameters, or you'll get an error message like i was unexpected at this time.
FOR /F documentation
Findstr documentation
If the processes name difference is not very complex, e.g. if the name is always the same
you can use the /FI option of taskkill directly
taskkill /FI "IMAGENAME eq your_image_name_here.exe"
==> taskkill documentation
I used this in command line:
name variable can contains blank surround with "
SET name="process name you want to kill" && FOR /F "tokens=2,* delims=," %f IN ('TASKLIST /fo csv /v ^| FINDSTR /i /c:!name!') DO #TASKKILL /f /t /pid %f
I need to be able to get the PID from a running process (cmd.exe) using the command line.
The problem is there are two cmd.exe running. One is under the username SYSTEM and one is compUser. Is there a way that I can grab the PID of the compUser cmd.exe?
Edit: This needs further explanation.
I'm doing this from a batch file. One of the calls that I'm making in my batch file starts a cmd.exe that never dies. So killing that cmd.exe would be simple:
taskkill /F /IM cmd.exe /FI "username eq compUser"
The problem is that the batch file that I'm in is being handled by another instance of cmd.exe under the username compUser. What I'm attempting to do is get the PID from the original cmd.exe before I start the second cmd.exe. That way I can just use the command:
taskkill /F /IM cmd.exe /FI "username eq compUser" /FI "PID neq [orignal task's PID]"
The way I ended up having to do this was use:
TASKLIST /NH /FI "IMAGENAME eq cmd.exe" /FI "username eq compUser"> psid.txt
FOR /F "tokens=2" %%I in (psid.txt ) DO set pIdNotToKill=%%I
right before I started the batch script that hangs. Then when I was ready to kill the hanging cmd window:
taskkill /F /IM cmd.exe /FI "PID ne %pIdNotToKill%" /FI "username eq compUser"
There is probably a better way, but this worked.
This will display all processes named "cmd.exe" for the user "compUser":
tasklist /FI "IMAGENAME eq cmd.exe" /FI "USERNAME eq compUser"
I set the Title to my own cmd window, other cmd windows what I call or run by "start /b" I rename to other window title name. Next I detect PID, set MYPID and kill all other cmd with OTHER PID of my.
All on one file, working in other label loop with timeout dalay.
#(#Title TitleOfMyScript)
for /f "tokens=2" %%a in ('tasklist /fi "imagename eq cmd.exe" /fi "windowtitle eq TitleOfMyScript" /nh') do set MYPID=%%a
taskkill /FI "PID ne %MYPID%" /FI "IMAGENAME eq cmd.exe" /F
I'd like just to share my piece of code, maybe it will be useful for somebody. It is based on the comment from jiggawagga, but it can terminate many processes:
#ECHO off
SETLOCAL ENABLEDELAYEDEXPANSION
REM kill old server process
SET pIdNotToKill=
SET pIdFilter=
FOR /F "tokens=2" %%I IN (%SERVER_HOME%\psid.txt) DO (
IF NOT "%%I"=="No" (
SET pIdNotToKill=!pIdNotToKill! %%I
SET pIdFilter=!pIdFilter! /FI "PID ne %%I"
)
)
IF NOT "!pIdNotToKill!"=="" (
ECHO Killing all Java processes except%pIdNotToKill%
ECHO TASKKILL command will be called with the filter%pIdFilter%
TASKKILL /F /IM java.exe %pIdFilter%
)
DEL %SERVER_HOME%\psid.txt
TASKLIST /NH /FI "IMAGENAME eq java.exe" > %SERVER_HOME%\psid.txt
right before I start server process.