I have Process1,Process2,Process3 in tasklist I want to write for loop or loop back to repeat the same task to kill the ‘n' number process
Which I mention in batch script.
Below is sample script using to kill process
#Echo Off
Tasklist | Findstr /I “Process1”
IF ERRORLEVEL 1 GOTO :Killprocess
GOTO:EOF
:Killprocess
Taskkill /IM “Process1”
If you are trying to kill the same program over and over, just do tasklist in a cmd.exe, then find its PID tag.
Example:
Image Name PID Session Name Mem Usage
========================= ======== ================ ============
firefox.exe 26356 Console 139,352 K
regedit.exe 24244 Console 9,768 K
cmd.exe 18664 Console 2,380 K
conhost.exe 2528 Console 7,852 K
notepad.exe 17364 Console 7,892 K
notepad.exe 24696 Console 22,028 K
notepad.exe 25304 Console 5,852 K
explorer.exe 2864 Console 72,232 K
After that, just put the PID into your text. I would run the program as such:
#echo off
TASKLIST
TASKKILL /PID (your PID here) /F
TASKKILL /PID (PID 2) /F
This will kill your tasks automatically. You don't need to find the tasks. If the program isn't running, then it won't kill it.
If you have a process that keeps changing its PID, or doesn't run under the same session, then run it as such:
#echo off
TASKLIST
TASKKILL /IM (program name) /F
TASKKILL /IM (program name 2) /F
Again, the program won't close if it isn't running.
Hope this helps!
Find more info here...
Thanks everyone for suggestions....done with script
#echo off
set len=3
set obj[0]=chrome.exe
set obj[1]=vlc.exe
set obj[2]=MicrosoftEdge.exe
set i=0
:loop
if %i% equ %len% goto :eof
for /f "usebackq delims== tokens=2" %%j in (`set obj[%i%]`) do (
TASKLIST |FINDSTR /I "%%j"
IF ERRORLEVEL 0 TASKKILL /IM "%%j" /F)
set /a i=%i%+1
goto loop
Related
How do I check if Zoom.exe is still running and then if its running I want to kill it in batch scripting below is my code which isn't working, it does print that Process not running
QPROCESS "zoom.exe">NUL
IF (%ERRORLEVEL% EQU 0) (ECHO "Process running" & TASKKILL /F /IM "Zoom.exe") ELSE (ECHO "Process not running")
setlocal EnableDelayedExpansion
set "imgname=Zoom.exe"
set "opt=tokens=* delims="
echo:
set /a "count=0"
for /f "%opt%" %%i in ('tasklist /fi "imagename eq !imgname!" /fi "status eq running"') do (
echo %%i
set /a "count+=1"
)
set /a "count-=2"
echo:
echo There are !count! !imgname! processes running.
echo:
if "!count!" gtr "0" (call taskkill /f /fi "imagename eq !imgname!")
endlocal
EDIT:
some explanation, but it is very easy. Also, to better understand yourself, type tasklist /? and taskkill /? and check #Mofi 's suggestions.
Basically, with the for /f loop you "read" (end echo, as when normally calling a command) the output of a command (passed within ' ').
Meanwhile you count the output lines with set /a "count+=1". This is done since in this case the output format is somehow "fixed". So you cannot extend this sentence to other commands. That is, tasklist command is at least 2 lines (in case process is not running, try with a non-existent imagename for example), or more. If more, it means that process is running, so to count how many you take out first two lines (yes, still 2 lines, but with different information w.r.t. process not running, this is why in this case this works) i.e. set /a "count-=2".
At this point you only need to check how many lines you left in the output. If 0, no process is running, else, you kill it/them.
This is my code so far ive tested it and both programs open when .bat is run , What im after is the code to close program 2 when program 1 closes any help would be appriciated as i have no coding knowledge :(
#echo off
start "TestFolder" "C:\Program Files (x86)\testfolder\test1.exe" %1
start "Testfolder2" "C:\Program Files (x86)\testfolder2\test2.exe"
taskkill /f /im test2.exe
exit
You need to setup a FOR /F Loop to monitor the output of TaskList to check for the window title of the 2nd program (and you'll need to explicitly set the window title of that program to something unique to do that.)
Then you will need to use Taskkill to kill the other task.
#(SETLOCAL
echo off )
CALL :Main
( ENDLOCAL
EXIT /B )
:Main
start "TestFolder1" "C:\Program Files (x86)\testfolder\test1.exe" %1
start "Testfolder2" "C:\Program Files (x86)\testfolder2\test2.exe"
CALL :Loop
taskkill /f /im test2.exe
GOTO :EOF
:loop
SET /A "n=10"
TIMEOUT 10
tasklist /V /NH /FI "IMAGENAME eq test1.exe" | FIND /I "TestFolder1" &&(
GOTO :loop
)
GOTO :EOF
I am using in cmd: tasklist /FI "session name eq console" to get list of console process es running.
I need to know if there is more than one process with same name and taskkill them
Thanks for helping
Take a look into this
Kill all processes with the same name
You need to count every image name occurrence from tasklist command output, see next batch script commented code snippet:
#ECHO OFF
SETLOCAL EnableExtensions
rem remove all variables with ? prefix (question mark)
for /F "tokens=1 delims==" %%G in ('2^>NUL set ?') do SET "%%~G="
rem count
for /F "skip=1 tokens=1 delims=," %%G in ('
tasklist /FI "SESSIONNAME eq Console" /FO csv
') do set /A "?%%~G+=1"
rem use counters
for /F "tokens=1,2 delims=?=" %%G in ('2^>NUL set ?') do (
if %%H GTR 1 (
rem next echo for debugging purposes
echo %%G process counter = %%H
if /I NOT "%%G"=="cmd.exe" (
rem taskkill command is merely displayed for debugging purposes
ECHO taskkill /IM %%G
) else (
rem needs more elaborated code to avoid killing the script itself
echo retain %%G
)
)
)
Output:
==> D:\bat\SO\37639505.bat
chrome.exe process counter = 4
taskkill /IM chrome.exe
cmd.exe process counter = 2
retain cmd.exe
iexplore.exe process counter = 3
taskkill /IM iexplore.exe
powershell_ise.exe process counter = 2
taskkill /IM powershell_ise.exe
==>
I am trying to wait for the process to end, Can't use the "Start /W" because the process opens from another program.
So all in all, I need some kind of scanner to look for if the process in the WaitSession and then continue the code to the KillSession
#echo off
color 02
cd /D C:\Windows\System32
timeout -t 1
***WaitSession***
(Wait for this process to end) MightyQuest.exe
***KillSession***
taskkill /f /im PublicLauncher.exe
taskkill /f /im AwesomiumProcess.exe
Thanks
This should do it:
#echo off
color 02
cd /D C:\Windows\System32
timeout -t 1
SET TempFile="%Temp%\%RANDOM%.txt"
:WaitSession
REM Fetch the current process list. Store in a temp file for easy searching.
TASKLIST > %TempFile%
REM Reset the process "flag" variable.
SET "Process="
REM Check for a process with the target name by searching the task list for the target process name.
REM If output is returned, it will be put into the Process "flag" variable.
FOR /F "usebackq tokens=1 delims=-" %%A IN (`FINDSTR /I "MightyQuest.exe" %TempFile%`) DO SET Process=%%A
REM If nothing was returned, it means the process isn't running.
IF "%Process%"=="" GOTO KillSession
ECHO Process is still running.
REM Wait and then try again.
TIMEOUT /T 20
GOTO WaitSession
:KillSession
taskkill /f /im PublicLauncher.exe
taskkill /f /im AwesomiumProcess.exe
REM Cleanup.
DEL %TempFile% > nul
The idea here is you keep polling the active task list and when the target process is found, you delay for a few seconds and then try again.
Once it is not found in the task list, you jump to the KillSession steps.
Use wmic Windows Management Instrumentation Command
#echo off
color 02
rem why? cd /D C:\Windows\System32
timeout -t 1
:wait
TIMEOUT -t 3 /NOBREAK>nul
rem ***WaitSession***
rem (Wait for this process to end) MightyQuest.exe
for /F "tokens=*" %%G in (
'wmic process where "name='MightyQuest.exe'" get name'
) do if /i not "%%G"=="No Instance(s) Available." goto :wait
rem ***KillSession***
taskkill /f /im PublicLauncher.exe
taskkill /f /im AwesomiumProcess.exe
A vbscript
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
Set objEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM Win32_ProcessTrace")
Do
Set objReceivedEvent = objEvents.NextEvent
msgbox objReceivedEvent.ProcessName
Loop
Use Win32_ProcessTraceStop to only catch terminations.
I'm having problems with the firefox Flashplayerplugin eating up too much ram and lagging my system when it's not in use. The only solution I found was killing the flashplayerplugin while using firefox, uninstalling, reinstalling or a fresh firefox install or new profile doesn't solve it; however, it's becoming very tedious having to check taskmanager all the time and kill it and the flashplayerplugin always seems to start on it's own.
The question I have is if it's possible to create a batch file to check if FlashPlugin_11_8_800_94.exe is running and kill it after a period of time (5-10 seconds) and continue running the batch file actively, in a loop, scanning if FlashPlugin_11_8_800_94.exe has started again, then kill it after 5 - 10 seconds, rinse and repeat?
Edit:
Found a batch file and modified it, but also seems to be missing some perimeters to actively search if it's running, even when it is not. It doesn't work either way though.
#echo off
:search
TASKLIST|FIND "FlashPlayerPlugin"
IF %ERRORLEVEL% = 0 THEN (GOTO found)
TIMEOUT /T 5
GOTO search
:found
taskkill /im FlashPlayerPlugin_11_8_800_94.exe
--
This batch file doesn't work either.
set tasklist=%windir%\System32\tasklist.exe
set taskkill=%windir%\System32\taskkill.exe
-------------------------------------------------------
:STOPPROC
set wasStopped=0
set procFound=0
set notFound_result=ERROR:
set procName=%1
for /f "usebackq" %%A in (`%taskkill% /IM %procName%`) do (
if NOT %%A==%notFound_result% (set procFound=1)
)
if %procFound%==0 (
echo The process was not running.
goto :EOF
)
set wasStopped=1
set ignore_result=INFO:
:CHECKDEAD
"%windir%\system32\timeout.exe" 3 /NOBREAK
for /f "usebackq" %%A in (`%tasklist% /nh /fi "imagename eq %procName%"`) do (
if not %%A==%ignore_result% (goto :CHECKDEAD)
)
goto :EOF
-------------------------------------------------------
:MAIN
call :STOPPROC FlashPlayerPlugin_11_8_800_94.exe
taskkill /im FlashPlugin_11_8_800_94* /f >nul 2>&1
For anyone who still might find it useful:
This is a small script that scans tasklist for processes containing processname once every 5 seconds. For example, if you put "notepad" for the processname, it will end processes like "notepad.exe" and "notepad++.exe". To use the script, copy and paste the following into notepad and save has "simple_pk.cmd". processname can have any characters except double quotes("), ampersands (&), or commas (,).
::Simple monitor and kill process
#echo off&prompt :&mode con cols=50 lines=10
set processname=flashplayerplugin
:loop
cls&echo Searching for %processname%...
for /f "tokens=1 delims=," %%a in ('tasklist /fo csv ^|FINDSTR /I /C:"%processname%"') do call :killprocess %%a
ping -n 6 127.0.0.1>NUL
goto :loop
:killprocess
echo. |set /p d=killing %*...
taskkill /f /im "%*">nul 2>&1
set err=%errorlevel%
set success=Success
if not %err%==0 set success=fail (err code: %err%)
if %err%==128 set success=fail (process not found)
echo %success%&goto :eof
This is a slightly different version of the same script. This will only end processes that match the whole name exactly:
::Simple monitor and kill process (exact name)
#echo off&prompt :&mode con cols=50 lines=10
set processname=FlashPlayerPlugin_11_8_800_94.exe
:loop
cls&echo Searching for %processname%...
for /f "tokens=1 delims=," %%a in ('tasklist /fo csv ^|FINDSTR /C:"%processname%"') do call :killprocess %%a
ping -n 6 127.0.0.1>NUL
goto :loop
:killprocess
set name=%*
set name=.,;%name:"=%;,.
echo %name%|FINDSTR /C:".,;%processname%;,.">nul || goto :eof
echo. |set /p d=killing %*...
taskkill /f /im "%*">nul 2>&1
set err=%errorlevel%
set success=Success
if not %err%==0 set success=fail (err code: %err%)
if %err%==128 set success=fail (process not found)
echo %success%&goto :eof
Your IF is not correct:
#echo off
:search
TASKLIST|FIND "setup.exe"
IF %ERRORLEVEL% equ 0 (GOTO found)
TIMEOUT /T 5
GOTO search
:found
taskkill /im setup.exe
More simpler form
#echo off
:search
TASKLIST|FIND "setup.exe"
IF %ERRORLEVEL% equ 0 (taskkill /im setup.exe
exit)
TIMEOUT /T 5
GOTO search
I was looking over the first bit of code at top and I find a way, I believe. This is what I got; really simple.
#echo off
cls
:start
timeout /t 5
tasklist|find "explorer.exe"
goto found
goto start
:found
taskkill /f /im explorer.exe
goto start
Of course, any program will work.