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 😎
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"
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
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 need help writing batch code.
In the initial state of my batch script I need to check if notepad.exe is running
if it is running then taskkill /im notepad.exe elsif notepad.exe is not running then go to next batch statement/code.
You can simply execute taskkill /im notepad.exe in all cases. If it's not running, then taskill will having nothing to kill and will just return.
In that situation, taskkill will report an error and set the error level. You can suppress the reporting of the error by redirecting standard error:
taskkill /im notepad.exe 2> nul
As for the error level, you can just ignore that and it will be cleared by the next command that you execute. Or if needed, you can clear it yourself.
This approach is, in my view, better than trying to anticipate whether or not taskkill will succeed. You won't be able to anticipate all possible failure modes and since taskkill itself performs the very check that you are asking about, I think you may as well leave that check to taskkill.
Try taskkill /fi "IMAGENAME eq notepad.exe"
Not finding notepad.exe will only throw an info instead of an error.
I like to run two programs using batch file, but the condition is, the second program must start only after the first program loaded, so is there any way to control using timer to control when the program starts.
I needed the same thing, and found out that following thing works as hoped:
start D:\Michal\Xming\Xming.exe -multiwindow
start D:\Michal\Xming\putty.exe
start D:\Michal\WinSCP\WinSCP.exe
And it all is saved in file Login.bat.
BTW, I am running Win7 but I doubt that this has any impact.
Basically, you could try this approach (not tested):
Run the first program using the start command.
Check the task list in a loop to see if the program has appeared there.
Impose some time limitation to the said loop.
Run the next program in case of success, exit with notification otherwise.
The scripting might look like this:
#ECHO OFF
START program1.exe
FOR /L %%i IN (1,1,100) DO (
(TASKLIST | FIND /I "program.exe") && GOTO :startnext
:: you might add here some delaying
)
ECHO Timeout waiting for program1.exe to start
GOTO :EOF
:startnext
program2.exe
:: or START program2.exe
Keep in mind that the timing is not precise, especially if you are going to insert delays between the task list checks.
I think this might be irrelevant here but would like share the following:
I 've created the following batch file and run it whenever I open my laptop in the office to open relevant programs at a single click.
Kept this file at Desktop and created a folder where I put all shortcuts for relevant programs.
So, I run these shortcuts in the batch file as follows:
#ECHO off
start C:\Users\User1\Desktop\Softwares\IE
start C:\Users\User1\Desktop\Softwares\Googletalk
start C:\Users\User1\Desktop\Softwares\YahooMessenger
start C:\Users\User1\Desktop\Softwares\Program4
start C:\Users\User1\Desktop\Softwares\Program5
I have also find a small hack to do it, just using a ping command with -n switch as follows:
start /d "C:\Program Files (x86)\Mobile Partner\" MobilePartner.exe
ping 127.0.0.1 -n 8
start /d "F:\Other Applcations\System Tools\OS Tweak\" dragfullwindows.exe
I wrote this answer as I am on Windows 10 and it is 2021.
And this provides a few more ideas for newage programs, that should run in the taskbar(in the background).
Here is my "Work Start.bat" batch file sitting on my desktop:
rem Work Start Batch Job from Desktop
rem Launchs All Work Apps
#echo off
rem start "Start VS Code" "C:\Users\yourname\AppData\Local\Programs\Microsoft VS Code\code.exe"
start "Start OneDrive" "C:\Users\yourname\AppData\Local\Microsoft\OneDrive\OneDrive.exe"
start "Start Google Sync" "C:\Program Files\Google\Drive\GoogleDriveSync.exe"
start "Start Clipboard" "C:\Program Files\Beyond Compare 4\BCClipboard.exe"
start "Start Cisco AnyConnect" "C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpnui.exe"
start chrome
start firefox
start skype
start "Start Teams" "C:\Users\yourname\AppData\Local\Microsoft\Teams\current\Teams.exe" /MIN
start Slack
start Zoom
sleep 10
taskkill /IM "explorer.exe"
taskkill /IM "teams.exe"
taskkill /IM "skype.exe"
taskkill /IM "slack.exe"
taskkill /IM "zoom.exe"
taskkill /IM "cmd.exe"
#echo on
Some Apps would not start with a simple "start app" command, so I used the full path.
For some reason some were found in my user appdata folder and not in program files, I do not understand this behaviour of program storage, it makes no sense.
I used a time delay so that the apps could fully start before I sent them to the background using taskkill command
I killed explorer.exe because OneDrive opens explorer
I killed cmd.exe because it opened and stayed opened due to badly behaving apps.
The rest I killed so that they would just move to the background.
Here is my "Work End.bat" batch file to forceably close everything:
rem Work End Batch Job from Desktop
rem Forcibly Closes All Work Apps
#echo off
taskkill /f /IM OneDrive.exe
taskkill /f /IM GoogleDriveSync.exe
taskkill /f /IM BCClipboard.exe
taskkill /f /IM "vpnui.exe"
taskkill /f /IM "chrome.exe"
taskkill /f /IM "firefox.exe"
taskkill /IM "explorer.exe"
taskkill /f /IM "teams.exe"
taskkill /f /IM "skype.exe"
taskkill /f /IM "slack.exe"
taskkill /f /IM "zoom.exe"
#echo on
I do have to ensure I have saved all my work, and that files are no longer syncing.
Possibly I will need a batch file that kills everything except file sync.
The good thing about forceably killing Chrome and firefox is they ask to be restored on next start, so I can continue where I left off, assuming I saved everything.
If I don't forceably kill these they stay in the background, if I close using the Cross they do not offer me to start from where I left off.
I could then start my gaming files, in another batch file but this would be similar to the first file.
Now I can turn off the default "Start Up" Apps and use this "Work Start.bat" , because the Start Up Apps annoy me when I start my pc just to game.