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%
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 have to find a processid for a specific jar file and kill it using the same batch file.
C:\Users\k9>ps -ef|grep java
10892 5648 0 Mar 08 con 0:02 "C:\Program Files\Java\jdk1.7.0_51\bin\java" com.mkwebappserver.MainClass -app CATMkWebAppServerConsole\apps_list.html -autobui
13060 7828 0 09:42:28 con 49:05 java -Djsse.enableSNIExtension=false -Djava.util.logging.config.file=..\config\log.properties -classpath "../extlib/*";..\extlib\mysql-connector-
here i want to find pid of config\log.properties file and kill it using the batch file.
This is how I "find" and kill Adobe Acrobat
::Close acrobat and any opened PDF
taskkill /im acrobat.exe /t /f
timeout 2
/im for imagename, or program you're targeting
/t terminates the specified process and any child process which were started by it
/f Forcefully terminate the process
The most flexible way to detect a pid by its command line (where your jar should be included) is with wmic. To process the wmic result and assign it to a variable you'll need FOR /F :
#echo off
for /f "useback tokens=* delims=" %%# in (
`wmic process where "CommandLine like '%%my_jar.jar%%' and not CommandLine like '%%wmic%%' " get ProcessId /Format:Value`
) do (
for /f "tokens=* delims=" %%a in ("%%#") do set "%%a"
)
echo %processId%
echo taskkill /pid %processId% /f
Where you should change '%%my_jar.jar%%' with your jar name.To kill the process you'll have to delete the echo word in the last line. It is echoed in order to check if the correct process id is handled.
For more info:
WMIC
FOR /F
WQL
TASKKILL
I am trying to get a song to play in the background of Windows, after a little looking I found this:
#echo off
set file=RRLJ.mp3
( echo Set Sound = CreateObject("WMPlayer.OCX.7"^)
echo Sound.URL = "%file%"
echo Sound.Controls.play
echo do while Sound.currentmedia.duration = 0
echo wscript.sleep 100
echo loop
echo wscript.sleep (int(Sound.currentmedia.duration^)+1^)*1000) >sound.vbs
start /min sound.vbs
This works well for starting the song but I have no way of stopping it from a .bat file. The only way I found to cut it short is to open task manager and close it from the processes.
I have tried:
taskkill /im wscript.exe
But I keep getting something in the window saying:
Success: Sent termination sidnal to the process "wscript.exe" with PID 185448
but the music continues to play until I manually end it with task manager
I'd use /T switch (Tree kill): terminates the specified process and any child processes which were started by it.
Here is a script to find ProcessID to terminate exactly needed process only using PID:
#ECHO OFF >NUL
for /F "usebackq tokens=*" %%G in (
`wmic process where "CommandLine like '%%sound.vbs%%' AND Caption like '%%script.exe%%'" get ProcessID/value ^|find /I "="`
) do (
rem echo %%G
for /F "tokens=2 delims==" %%H in ("%%~G") do echo taskkill /T /F /PID %%H
)
Note taskkill command is echoed merely... Remove echo when debugged.
This work for me, and is only one line.
Taskkill /IM "wscript.exe" /F>nul 2>&1
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
I'm capturing the PID in a variable which I kill later
IF NOT "%SERVICE_PID%" == 0 taskkill /pid %SERVICE_PID% /t /f
though everytime I do this in a batch file it makes my computer restart because it kills some system process
the service pid should be a user defined service launched from cmd
I dont understand why it keeps making my machine croak.
When I run "taskkill /pid %SERVICE_PID% /t /f" on the command line it works fine! =/
help!
Setting SERVICE_PID
FOR /F "tokens=4 delims= " %%A IN ('sc queryex myservice ^|FIND "PID"')
DO SET SERVICE_PID=%%A
I found that using this will get the proper PID:
for /f "tokens=1,2,3,4 delims=/ " %%a in ('sc queryex ServiceName ^|FIND "PID"') do set PID=%%c
taskkill /pid %PID% /t /f
Then it works like a charm.
Try removing /f option, it forces to terminate a process, so system processes might get terminated forcefully without notification.
I am suspecting you are trying to kill some system processes in the batch script, in the sense that in your list of PIDs there might be some system process IDs as well.
Thanks
Make sure you have enabled delayed expansion. See the HELP SET for an explanation.
Insert this line
SETLOCAL ENABLEDELAYEDEXPANSION
as the first line of your batch file.
and use !SERVICE_PID! instead of %SERVICE_PID% to get the environment variable.
taskkill /fi "IMAGENAME eq idsm.exe" /fi "CPUTIME gt 00:30:00"
Image Name PID Session Name Session# Mem Usage
========================= ======== ================ =========== ============
idsm.exe 4556 Console 0 38,328 K
I got the this result, in the same command line how to kill this process, its should run automatically, means how to use this PID for killing
I found this the most reliable
net stop SERVICE_NAME
timeout /t 10
for /f "tokens=1,2,3,4 delims=/ " %%a in ('sc queryex SERVICE_NAME ^|FIND "PID"') do set PID=%%c
if not %PID% == 0 taskkill /pid %PID% /f
timeout /t 10
net start SERVICE_NAME
The timeout is arbitrary.
The first timeout gives service a chance to stop itself properly for some extra amount of time.
The second timeout allows the operating system to react, otherwise you may end up with
The service is starting or stopping. Please try again later. and service will not restart.
I am checking if service is not stopped by if not %PID% == 0 and force killing it if necessary. The /t flag was unnecessary in my case.