Any way to write a Windows .bat file to kill processes? [closed] - performance

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Every time I turn on my company-owned development machine, I have to kill 10+ processes using the Task Manager or any other process management app just to get decent performance out of my IDE. Yes, these are processes from programs that my company installs on my machine for security and compliance. What I'd like to do is have a .bat file or script of some kind with which I can kill the processes in question.
Does anybody know how to do this?

You can do this with 'taskkill'.
With the /IM parameter, you can specify image names.
Example:
taskkill /im somecorporateprocess.exe
You can also do this to 'force' kill:
Example:
taskkill /f /im somecorporateprocess.exe
Just add one line per process you want to kill, save it as a .bat file, and add in your startup directory. Problem solved!
If this is a legacy system, PsKill will do the same.

taskkill /f /im "devenv.exe"
this will forcibly kill the pid with the exe name "devenv.exe"
equivalent to -9 on the nix'y kill command

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 its SQL-like matchers .With wmic Path win32_process get you can see the available fileds you can filter (and % can be used as a wildcard).

I'm assuming as a developer, you have some degree of administrative control over your machine. If so, from the command line, run msconfig.exe. You can remove many processes from even starting, thereby eliminating the need to kill them with the above mentioned solutions.

Get Autoruns from Mark Russinovich, the Sysinternals guy that discovered the Sony Rootkit... Best software I've ever used for cleaning up things that get started automatically.

Download PSKill. Write a batch file that calls it for each process you want dead, passing in the name of the process for each.

Use Powershell! Built in cmdlets for managing processes. Examples here (hard way), here(built in) and here (more).

Please find the below logic where it works on the condition.
If we simply call taskkill /im applicationname.exe, it will kill only if this process is running. If this process is not running, it will throw an error.
So as to check before takskill is called, a check can be done to make sure execute taskkill will be executed only if the process is running, so that it won't throw error.
tasklist /fi "imagename eq applicationname.exe" |find ":" > nul
if errorlevel 1 taskkill /f /im "applicationname.exe"

Here I wrote an example command that you can paste in your cmd command line prompt and is written for chrome.exe.
FOR /F "tokens=2 delims= " %P IN ('tasklist /FO Table /M "chrome*" /NH') DO (TASKKILL /PID %P)
The for just takes all the PIDs listed on the below tasklist command and executes TASKKILL /PID on every PID
tasklist /FO Table /M "chrome*" /NH
If you use the for in a batch file just use %%P instead of %P

Related

How can I stop one instance via cmd of a process when several are running?

I start a program from a scilab script via the command line, start myprog.exe.
After the start my scilab script needs to keep going.
Now I want to stop exactly this instance of the process via the command line too, even if several instances of the same program are running.
Is that possible?
I know how to query via batch files whether a process of this program is running and then stop it, but I don't know how to get the exact allocation.
Is there something like a process id?
I use this to check if the process is running:
tasklist /fi "imagename eq ccx.exe" |find ":" > nul
if errorlevel 1 echo Program is running
if not errorlevel 1 echo Program is not running
Use the command tasklist to view all running tasks with their PID
then
Taskkill /PID 26356 /F
or
Taskkill /IM myprog.exe /F

TASKKILL specific Python script

I have a working TASKKILL command that kills python.exe using Process Name
I'd like to narrow the scope of the command to kill a specific process (myScript.py) but can't use ProcessID as it changes with every run.
Is there a way I can add detail from the Command Line which knows the python script's name?
Current Command:
Taskkill /IM python.exe /F >nul 2>&1
if errorlevel 1 (echo PYTHON.exe NOT FOUND) else (echo PYTHON.exe KILLED)
You might be able to kill it based on your Python script's memory usage. In my case, the Python script is running a GUI, so the size gives it away.
taskkill /f /fi "IMAGENAME eq python.exe" /fi "MEMUSAGE gt 130000"
This reads as, forcefully kill the task (taskkill /f) identified by (/fi) the Python executable (IMAGENAME eq python.exe) which is using more than 130,000KB (MEMUSAGE gt 130000)1.
1 See taskkill /? for builtin help.
N.B. You might find this SO post helpful: Find Windows PID of a python script with Windows Command Prompt. Unfortunately, for me, It Doesn't Work™ but maybe you will have better luck.

Closing a window which pops up while running batch file

There is this application build process that I am trying to automate. For this i wrote a java file, which runs every 24 hours.
A batch file is called from here that runs the application build whenever it is called.
I've run into a small problem, when the build fails due to incomplete or invalid files, a window pops up which tells me to look at the logs.
Since I haven't written the build files, I'm not really sure where this gets created from. I wanted to know if I can close this window while the process runs from the bat file.
It may be possible using taskkill, but you'd have to devise a filter that would ideally only match the process displaying the window and never match any other process. Something like:
taskkill /im program.exe
or maybe:
taskkill /fi "windowtitle eq title*"
You might also want to include the /f flag for forceful termination.
You'd also have to try and make sure that the taskkill command doesn't run too quickly and precede the creation of the popup window. You could try to query for the existence of such a process/window; your best bet here is probably wmic. Maybe:
#echo off
setlocal enabledelayedexpansion
set title=Notepad
set pid=
for /f %%i in ('wmic process where "caption like \"%%!title!%%\"" get processid^| findstr /r [0-9]') do #set pid=%%i
if "!pid!" neq "" taskkill /f /pid !pid!
There's no guarantee this will always work, but it's probably the best you can do.

Kill a windows service using its Service Name

I am looking for a way to kill a windows services using its service name rather than the process name, or PID. The two obvious choices are pskill or taskkill, but I cannot seem to find a way of using either of these methods to kill the service by name.
Is it possible to do it by the service name? If so, is anyone able to provide a quick example?
You can use taskkill to filter by service name and kill the service you're looking for.
taskkill /F /FI "SERVICES eq yourservice"
Do you actually want to KILL the process (e.g. if it is frozen) or do you want to STOP the service?
If you want to kill it stick to SomethingDark's answer (taskkill /F /FI "SERVICES eq yourservice").
If you want to stop it use SC STOP "servicename".
In case you have multiple services with the same image name, using the SERVICES filter may not be sufficient. In this case, you need to use more filters like IMAGENAME, PID, STATUS or more.
See my related answer here
with taskkill loop (be careful with closing system processes) (save winservices.bat and run as admin)
call :winservices "service_name1"
call :winservices "service_name2"
:: funcion winservices
#echo off
goto:eof
:winservices
set winservices=%1
taskkill /f /im "%winservices%" /t
goto:eof
source: https://serverfault.com/questions/1005487/how-to-stop-start-and-delete-a-windows-service-with-a-reference-to-the-service

Attaching the windows debugger in VS2010 from a batch file?

Is it possible to attach the windows debugger in VS2010 to a process from a batch file?
preferably by giving it a process name
Since you presumably already have the process running, you would use vsjitdebugger.exe /p 1234 where 1234 is the PID of the process you want to debug. If you don't know it, you would have to use some other method to figure it out.
If you have the debugging tools for windows available, the tlist.exe utility will yield the process ID for a process name. If that is available, then the following will attach to a given process:
rem Get the process ID
for /f %%f in ('tlist -p %1') do set mypid=%%f
rem attach to it with selected debugger
vsjitDebugger -p %mypid%
Edit If tlist is not available, I think tasklist will work. It's a bit uglier, but the following worked for me (you know ... it works my on my system :) Note too that I edited the command previous example to work in a cmd.exe prompt (I use tcc, which does require as many % signs).
rem Get the process ID
for /f "tokens=2 delims= " %%f in ('tasklist /nh /fi "imagename eq %1"' ) do set mypid=%%f
rem attach to it with selected debugger
vsjitDebugger -p %mypid%
Specifying a /Command switch on devenv.exe 's command-line will make it run a specified command on open. You could specify the Debug.AttachToProcess command. Don't know if you can specify a pid, though, when you execute that command.

Resources