I try to close all firefox if there are more than one. I want to do loop that check the firefox process and close and check again until there is no any firefox process.
This is my code:
:loop
taskkill /im "firefox.exe"
tasklist /fi "imagename eq firefox.exe" goto loop
Where is my wrong in this command ? Any help is appreciated.
:loop
tasklist /fi "imagename eq firefox.exe" | find "firefox.exe" >nul && ( taskkill /im firefox.exe >nul & goto loop )
Get list of tasks, and if firefox.exe included in it, then kill it and goto loop
You can also use the following to force kill FF windows:
taskkill /F /FI "ImageName eq firefox.exe"
One taskkill command will kill every firefox process - unless it is not responding.
You could use two taskkill commands separated by a delay of several seconds, and the second one should use the /f force switch.
I do
#echo off
TASKKILL /IM firefox.exe /F
pause>nul
exit
I'm on windows 7, it works for me
Related
I wrote a script(working) which kills every task which is not equal with the windowtitle DEFENDINGSOFTWARE.
So it is:
Taskkill /FI “WINDOWTITLE ne DEFENDINGSOFTWARE*“ /F
How can I manage that he is also not killing the tasks which Name is “DEFENDINGSOFTWARE1”?
The * already takes care of that.
Taskkill /FI "WINDOWTITLE ne DEFENDINGSOFTWARE*" /F
should kill
DEFENDINGSOTWARE
DEFENDINGSOTWARE1
DEFENDINGSOTWARE2
DEFENDINGSOTWARE - some text
As an example do from cmdline:
start "Test" pause
start "Test2" pause
You will have 2 titled cmd windows called
Test - pause
Test2 - pause
now run
taskkill /FI "WINDOWTITLE EQ Test - pause"
which will only kill Test - pause
but running:
taskkill /FI "WINDOWTITLE EQ Test*"
Kills all windows with a title starting with Test
Also, by running taskkill /? from cmdline you will see the filter switch help.
/FI filter Applies a filter to select a set of tasks.
Allows "*" to be used. ex. imagename eq acme*
with examples further down, where I selected the most relevant example, demonstrating this behaviour.
TASKKILL /F /FI "PID ge 1000" /FI "WINDOWTITLE ne untitle*"
I trying to do an infinite loop to check if node.exe is using over some number of memory. Like this:
:loop2
sleep
taskkill /f /im node.exe "memusage gt 85000" > nul
timeout /t 30
GOTO loop2
But how can I break this infinite loop, when the taskkill is processed?
set "Myprocess=node.exe"
:test
tasklist | find /i "%MyProcess%">nul && Taskkill /F /IM "%MyProcess%" & exit/b
timeout /t 30
goto:test
:loop2
sleep
( taskkill /f /im node.exe /fi "memusage gt 85000" | find "PID" >nul ) || (
timeout /t 30
GOTO loop2
)
Check the output of the command to determine if a process has been killed.
Edited I thought the PID string was locale independent, but it is not. If needed, the find command can be replaced by
find /i "node.exe" > nul
to search for the name of the process in the output, or
findstr /r /c:" [1-9][0-9]*[02468][\., ]" > nul
to search for the presence of a process ID (note: all process IDs are multiple of 4)
Works for me, for any locale.
:loop2
sleep
set _tk=
for /f "skip=1 delims=, tokens=2" %%A in ('tasklist /fi "imagename eq node.exe" /fi "memusage gt 85000" /fo csv') do (
taskkill /f /pid %%~A
set _tk=1
)
if not "%_tk%"=="1" (
timeout /t 30
GOTO loop2
)
Another approach would be to use TASKLIST to see if a matching image is running. This code is -not- tested.
:loop2
SET TEMPFILE=%TEMP%\tk_%RANDOM%.tmp
TASKLIST /FI "IMAGENAME eq cmd.exe" /FI "MEMUSAGE gt 9000" >"%TEMPFILE%"
SET /P TKI=<"%TEMPFILE%"
IF "%TKI%" EQU "INFO: No tasks are running which match the specified criteria." (GOTO OutOfIt)
taskkill /f /im node.exe "memusage gt 85000" > nul
timeout /t 30
GOTO loop2
:OutOfIt
IF EXISTS "%TEMPFILE%" (DEL "%TEMPFILE%")
EXIT /B
taskkill /IM notepad.exe /FI "memusage gt 999" |find "SUCCESS" &&GOTO :BREAKOUT
echo off
:loop
tasklist /fi "imagename eq Notepad.exe" | find "INFO:" > nul
if errorlevel 1 goto loop
wordpad.exe
This is not working in XP. It works fine in Windows 7.
When notepad.exe does not exist in the task list, tasklist /fi "imagename eq Notepad.exe" dumps the "INFO:" line to stderr in Windows XP. You could redirect stderr to stdout with 2>&1, but it's easier just to find /i "notepad" instead.
On a side note, instead of doing if errorlevel 1 you could use conditional execution.
#echo off
setlocal
:loop
rem // Output nothing for the following code block.
>NUL 2>NUL (
rem // Make sure notepad is not running before continuing.
tasklist /fi "imagename eq notepad.exe" | find /i "notepad" && (
rem // Notepad is in tasklist. Sleep 1 second, then check again.
timeout /t 1 /nobreak || ping -n 2 localhost
goto loop
)
)
wordpad.exe
I need a batch (.bat) file that opens a program if it's not open, and stops the program if it is open. I have a game where when the launcher is closed, it stays open in the background. And I have to end it with task manager or else I can't launch it because steam doesn't like it when an app is open two times (it doesn't allow it), so I would like a batch file that does this for me, then bind it to a macro.
To check if your programm is running : (Here an example with notepad.exe)
#echo off
Set "MyProcess=Notepad.exe"
tasklist | find /i "%MyProcess%">nul && echo %MyProcess% Is running || echo %MyProcess% Is not running
So you can do like that :
#echo off
Set "MyProcess=Notepad.exe"
tasklist | find /i "%MyProcess%">nul && Taskkill /F/IM "%MyProcess%" || start "%MyProcess%"
This is another way to do it:
#echo off
tasklist /fi "imagename eq Launcher.exe" |find "." > nul && taskkill /f /im "Launcher.exe" & goto :EOF
tasklist /fi "imagename eq Launcher.exe" |find "." > nul || start "" steam://rungameid/243870 & goto :EOF
So I figured this out:
#echo off
tasklist /fi "imagename eq Launcher.exe" |find ":" > nul
if errorlevel 0 taskkill /f /im "Launcher.exe" && exit
start steam://rungameid/243870
exit
The logic behind it is that if you can kill the process, then end the prompt before you can create a new session of the process. But if the process can't be killed, then make a new session of it, then end the prompt.
I am in need of help to adding a not responding check to my .bat and restarting a server application when it is not responding. This is what I have so far.
#echo on
tasklist /nh /fi "imagename eq javaw.exe" | find /i "javaw.exe" >nul && (
echo Server is running.
//
I need a not responding check right afterward here. If the program is not responding,
taskkill /f /IM server.exe
taskkill /f /IM javaw.exe
cd C:\Users\Administrator\Desktop
serverstart.jar
else
exit
//
exit
) || (
echo Server process not found.
taskkill /f /IM server.exe
taskkill /f /IM javaw.exe
cd C:\Users\Administrator\Desktop
serverstart.jar
)
pause>nul
It is necessary for me to kill both of these process because it doesn't end eachother when it crashes apparently.
You already managed to filter by imagename.
You can also filter by Status (Running / Not Responding / Unknown):
tasklist /nh /fi "imagename eq java.exe" /fi "status eq not responding"
will give you any java.exe, which does not respond.
EDIT
well - when I think about it: I would try:
tasklist /nh /fi "imagename eq javaw.exe" /fi "status eq running` |find /i "javaw.exe" >nul && (
echo Server is running
rem nothing to do
) || (
echo Server is not running or not responding
rem restart service
)