When using task scheduler, if you run a batch program, when ending the script, it only closes the batch file, and not the program spawned by the batch file.
Is there a way to close all programs opened by that batch session when it gets a terminate command?
e.g.
echo started: %date% %time% >> log.txt
calc
timeout 5
echo ended: %date% %time%
taskkill /im win32calc.exe
Is there a way in batch to run the taskkill command (or another way to close all spawned processes) when it tries to get force closed.
In python, this would be done with a try/finally, like this:
try: ... finally: killtask('win32calc')
where the finally always runs no matter what.
Or with open 'calc.exe': # run some code
I think the behavior you want can be implemented via a modification of this answer, that is:
rem Start the process that will kill the calc.exe program after 5 seconds
start "WaitingToKill" cmd /C timeout /t 5 ^& taskkill /im calc.exe /f
rem Run the calc.exe program and wait for it to terminate
calc.exe
rem If calc.exe ends before 5 seconds, kill the killer process and terminate
taskkill /fi "WINDOWTITLE eq WaitingToKill" /f
Related
I have to write a batch file which is suppose to kill all the currently running CMD processes, except one which is in Listening mode before regression runs. If there is any opened CMD in the same path as Regression, it ruines the regression. There would be probably 2 ways to achieve this:
1. Close all command prompts and start the listener (which can be done by calling a soft link).
2. Close all command prompts, except the one which would run the Regression.
I tried to kill the command prompts, however it ruined the regression as it killed the cmd which is expected to run the regression.
TASKKILL /F /IM cmd.exe /T
cd %appdata%\Microsoft\Windows\Start Menu\Programs\Startup
"Regression Run.lnk"
If I go ahead with approach 1, tried to kill all cmd instances and then run the link, as soon as TASKKILL is called the cmd running itself is also killed. The other 2 lines were never ever called.
For the other approach, I am not able to get any method how to add any exception while calling TASKKILL.
If any one has better idea around it or suggest a better way to achieve it.
Any guidance would be greatly appreciated.
This can be done by using TASKKILL /FI to filter out the PID of the current cmd.exe shell you are running. I would like to know an easier way of getting the current cmd.exe PID.
#ECHO OFF
SET "TEMPFILE=%TEMP%\regtest.tmp"
powershell -NoProfile -Command ^
"(Get-WmiObject -Class Win32_Process -Filter "processid=`'$pid`'").ParentProcessId" >"%TEMPFILE%"
SET /P "MYPID=" <"%TEMPFILE%"
ECHO MYPID is set to %MYPID%
IF EXIST "%TEMPFILE%" (DEL "%TEMPFILE%")
TASKLIST /FI "IMAGENAME eq cmd.exe"
TASKKILL /F /FI "PID ne %MYPID%" /IM cmd.exe
TASKLIST /FI "IMAGENAME eq cmd.exe"
I have two programmes, both are long running codes. I want to run them on command prompt parallelly and after some delay I want to kill one programm. how it could be done with batch file? I have tried the solution given How to set a timeout for a process under Windows 7?
the solution is killing the command prompt itself.
I have tried:
moniter_batch.bat
start note.py
start test.py
timeout /t 10
taskkill /im python.exe /f
note.py
import time
while True:
print "I am ok\n"
time.sleep(1)
test.py
import time
while True:
print "I am ok\n"
time.sleep(1)
I want to kill note.py only and test.py must continue but my batch file is killing both the programmes
Try getting a list of processes with:
TASKLIST /V /FO "CSV" | FINDSTR note
Sample Output
C:\>tasklist /v /fo "CSV" | FINDSTR note
"cmd.exe","828","Console","0","2,492 K","Running","WINXP\Person","0:00:00","C:\WINDOWS\system32\cmd.exe - note"
and then using the PID (in the second field) with TASKKILL:
TASKKILL /PID 828
On my server i run a batch that call "program.exe", this program processes some data and close. If it doesn't close i will need to kill it.
The problem is that i could have many "program.exe" on the same machine but i have to kill only mine. Is it possible?
The batch file is called from an ASP page and it is launched with LocalSystem user privileges and does the following:
c:
cd \infos_a\exe
set infos=c:\infos_a\files\paolor
start /LOW program.exe #C:\inetpub\wwwroot\infospnt\bat\01\
if program.exe run more than 30sec it must be killed
Taskkill help gives an example
taskkill /im program.exe /fi "Username eq MyName"
I'm executing programs in a batch file. I want to break if the batch script runs more than some time(say 10 sec). Because it might become an infinite loop.
cls
::set timeout here
program.exe
::after timeout continue from here
Is there any suggestion to do this in a windows batch file? Plus, is it possible to define the line of endless loop in a batch script?
When you run a program from a batch file, you either run it and wait for it to terminate before executing the next command in the batch file, or you launch the program and executing immediately continues in the batch file. There is no way to measure how long a program runs. However, you can use certain techniques to see if a program is running and terminate it. You can also insert delays. So you can do what you are asking (more or less):
cls
start program.exe
ping -n 10 -w 1000 127.0.0.1 > nul 2>&1
tasklist | find /i "program.exe"
if %errorlevel%==0 taskkill /im program.exe
This starts the program and immediately starts to execute the next line of code, the ping. The ping causes a 10 second delay. The tasklist command pipes the list to find, which looks for the program. If it is found in the list, then it is still running and errorlevel is set to 0, which means the next line terminates the program.
This isn't fool proof however, because if program.exe is a common program name, then tasklist and taskkill will terminate all processes with the same program name (not just the instance you started).
How about that?
start calc.exe
ping -n 10 localhost >nul
taskkill /F /IM calc.exe
So I have process I started from one bat file. How to stop it from another?
To terminate a process you know the name of, try:
taskkill /IM notepad.exe
This will ask it to close, but it may refuse, offer to "save changes", etc. If you want to forcibly kill it, try:
taskkill /F /IM notepad.exe
As TASKKILL might be unavailable on some Home/basic editions of windows here some alternatives:
TSKILL processName
or
TSKILL PID
Have on mind that processName should not have the .exe suffix and is limited to 18 characters.
Another option is WMIC :
wmic Path win32_process Where "Caption Like 'MyProcess.exe'" Call Terminate
wmic offer even more flexibility than taskkill .With wmic Path win32_process get you can see the available fileds you can filter.
When you start a process from a batch file, it starts as a separate process with no hint towards the batch file that started it (since this would have finished running in the meantime, things like the parent process ID won't help you).
If you know the process name, and it is unique among all running processes, you can use taskkill, like #IVlad suggests in a comment.
If it is not unique, you might want to look into jobs. These terminate all spawned child processes when they are terminated.
I just wanted to kill all instances of Chrome when the browser won't open (recent Chrome annoying bug). You can end up with a bunch of chrome.exe processes running from 2 to ?.
I just did a clean and quick check to see if it's running and kill it if it is. The pause at the end is only so I can view the results, it isn't needed.
#echo off
:again
taskkill /F /IM "chrome.exe"
if errorlevel=0 goto end
if errorlevel=1 goto again
:end
pause
it works well for me
taskkill /F /IM notepad.exe this is the best way to kill the task from task manager.
Edit:
call runntaskkill.bat is changed to call taskkillapp.bat or else it will end up with the same file.
You can make two batch files. One named runtaskkill.bat and another named taskkillapp.bat. To the file runtaskkill.bat add the following code:
call taskkillapp.bat [filenamehere].
And to taskkill.bat:
taskkill /f /im %1.
Just make sure that the two files are in the same directory (folder).
Why don't you use PowerShell?
Stop-Process -Name notepad
And if you are in a batch file:
powershell -Command "Stop-Process -Name notepad"
powershell -Command "Stop-Process -Id 4232"
Here is how to kill one or more processes from a .bat file.
Step 1. Open a preferred text editor and create a new file.
step 2. To kill one process use the 'taskkill' command, with the '/im' parameter that specifies the image name of the process to be terminated. Example:
taskkill /im examplename.exe
To 'force' kill a process, use the '/f' parameter which specifies that processes be forcefully terminated. Example:
taskkill /f /im somecorporateprocess.exe
To kill more than one process you repeat the first part of step 2 with the appropriate processes for the process name. Example:
taskkill /im examplename.exe
taskkill /im examplename1.exe
taskkill /im examplename2.exe
or
taskkill /f /im examplename.exe
taskkill /f /im examplename1.exe
taskkill /f /im examplename2.exe
Step 3. Save your file to your desired location with the .bat extension.
Step 4. Click the newly created bat file to run it.
To just click file and run with no message "press any key to continue"
#echo off
call taskkill /f /im adb.exe /im OfficeClickToRun.exe
pause>nul
exit /b 0
add any amount of processes:
/im the_process_name.exe
TIP:
Create a shortcut of the .bat file somewhere (you can drag it to start menu/all programs) , go to the shortcut file properties and add a shortcut key 😎