Listing and terminating processes from CLI

Discuss anything related to command line tools here.
Post Reply
Message
Author
User avatar
Midas
Posts: 6710
Joined: Mon Dec 07, 2009 7:09 am
Location: Sol3

Listing and terminating processes from CLI

#1 Post by Midas »

If, like me, you test a lot of programs you're are bound to have some of them locking up, which on occasion might be catastrophic. Inspired by the simplicity of Linux's XKILL command, here's a quick non-GUI tip I harvested from several sources that provides for a quick way to view (and terminate) any locked up program(s)...

1. Create a new shortcut on an easy to access location (mine is right in the system desktop) and paste the following in the 'Target' field:

Code: Select all

%COMSPEC% /K "TASKLIST /FI "STATUS eq NOT RESPONDING""
2. Name it anything you like (e.g., LISTPROCS).

Now whenever you double click this shortcut, it will spawn a console window with a list of non-responding processes (be aware that not every process listed is in fact locked up -- portable launchers, for instance, only appear so).

You can kill any of those by executing this command in the same console window:

Code: Select all

TASKKILL /PID [pidnumber] /F
You can even terminate multiple (or all) programs by entering their pidnumbers separated by spaces...

EDIT: or you might just copy+paste the following into a text file, rename it 'kill.cmd' and drop it somewhere on your path to be able to kill processes with a simple "killl XXXX" command...

Code: Select all

@TASKKILL /PID %1 /F
For more info, see http://ss64.com/nt/tasklist.html and http://ss64.com/nt/taskkill.html.

User avatar
Midas
Posts: 6710
Joined: Mon Dec 07, 2009 7:09 am
Location: Sol3

Re: Listing and terminating processes from CLI

#2 Post by Midas »

Here's a more complete routine I devised to detect and kill a program using TASKLIST & TASKKILL...

> killprog progname

Code: Select all

@ECHO OFF
REM killprog.bat by Midas, 2013
ECHO.
TASKLIST /FI "IMAGENAME eq %1" | FIND /I "%1" >NUL
IF %ERRORLEVEL% EQU 1 GOTO FAIL
IF %ERRORLEVEL% EQU 0 (
ECHO. & ECHO FOUND: %1 IS running. & REM && (
ECHO. & (SET /P "_kill=Kill %1? [y|N] " || GOTO END) & ECHO. && ^
IF "%_kill%"=="y" TASKKILL /IM %1) & GOTO END )

:FAIL
ECHO.
ECHO FAIL: %1 NOT running!

:END
ECHO.
PAUSE

For easy reference, I'm transcribing here a very comprehensive example of TASKKILL use in a batch script.

Found at: www.howtogeek.com/50630/batch-script-to-conditionally-restart-an-application/

Code: Select all

@ECHO OFF
ECHO Restart Application
ECHO Written by: Jason Faulkner
ECHO SysadminGeek.com
ECHO.
ECHO.

SETLOCAL EnableExtensions

REM Enter the application information.
SET AppName=Application Name
SET ExeFile=FileToLaunch.exe
SET ExePath=C:PathToApplication

REM Select the conditions to kill the application.
REM A value of 1 = Yes, 0 = No
SET KillIfRunning=1
SET KillIfNotResponding=1
SET KillIfUnknownStatus=1

REM Specify when to start the application:
REM 1 = Start only if the process was previous killed.
REM 0 = Start the application regardless.
SET StartOnlyIfKilled=1

SET KillStatus="%TEMP%KillStatus.tmp.txt"
SET Success=0

ECHO Killing existing %AppName% instance...
IF {%KillIfRunning%}=={1} CALL :CheckKillStatus "%ExeFile%" "RUNNING"
IF {%KillIfNotResponding%}=={1} CALL :CheckKillStatus "%ExeFile%" "NOT RESPONDING"
IF {%KillIfUnknownStatus%}=={1} CALL :CheckKillStatus "%ExeFile%" "UNKNOWN"
ECHO.

IF {%StartOnlyIfKilled%}=={1} (
	IF {%Success%}=={0} GOTO End
)
ECHO Restarting %AppName%...
START "%ExeFile%" "%ExePath%%ExeFile%"
ECHO.

IF EXIST %KillStatus% DEL /F /Q %KillStatus%

ENDLOCAL

:CheckKillStatus
ECHO Killing with status: %~2
TASKKILL /FI "STATUS eq %~2" /IM "%~1" /F > %KillStatus%
SET /P KillResult= < %KillStatus%
FOR /F "tokens=1,* delims=:" %%A IN ("%KillResult%") DO (
	ECHO %%A:%%B
	IF /I {%%A}=={SUCCESS} SET /A Success=%Success%+1
)

:End

User avatar
__philippe
Posts: 687
Joined: Wed Jun 26, 2013 2:09 am

Re: Listing and terminating processes from CLI

#3 Post by __philippe »

Very useful "Display and Kill" hung application CLI scripts, thanks.

In the same spirit, here is a tiny AHK (AutoHotKey) macro to kill any hung web page displayed under WinXP IE8 browser.

Code: Select all

;==== Kill misbehaving IExplorer runaway processes
;hotkey combo is "CTRL K"
^K::
run taskkill /f /im iexplore.exe ,,hide
exit
;
Just hit "CTRL K", and the offending page is "Terminated with Extreme Prejudice"...;-)
Never failed me.

__philippe
Last edited by __philippe on Sun Jul 07, 2013 12:18 pm, edited 1 time in total.

User avatar
Midas
Posts: 6710
Joined: Mon Dec 07, 2009 7:09 am
Location: Sol3

Re: Listing and terminating processes from CLI

#4 Post by Midas »

Thanks. If it wasn't for the AutoHotKey runtime requirement, that script would be the very definition of terse -- especially when compared with the length and obscurity of killprog.bat ... ;)

User avatar
__philippe
Posts: 687
Joined: Wed Jun 26, 2013 2:09 am

Re: Listing and terminating processes from CLI

#5 Post by __philippe »

Mind you, an equivalent unadorned batch file could be just a one-liner :

Code: Select all

"Taskkill /f /im iexplore.exe"
but then, in an emergency, the batch file itself would have to be invoked in some circuitous way,
whereby one is deprived the instant gratification of AHK 's wicked "CTRL K" summary execution... :wink:

__philippe

TP109
Posts: 571
Joined: Sat Apr 08, 2006 7:12 pm
Location: Midwestern US

Re: Listing and terminating processes from CLI

#6 Post by TP109 »

XP Home doesn't include TASKLIST OR TASKKILL. TASKLIST is supported in XP Home and can be downloaded from various sites and installed to the System32 directory. XP Home doesn't support TASKKILL, but does include TSKILL, which is a less powerful command than TASKKILL for terminating processes. TSKILL uses a similar, but slightly different syntax, so the batch files need to be changed to use TSKILL instead of TASKKILL if executing them on an XP Home PC.

User avatar
Midas
Posts: 6710
Joined: Mon Dec 07, 2009 7:09 am
Location: Sol3

Re: Listing and terminating processes from CLI

#7 Post by Midas »

Thanks for improving the topic, TP109. 8)

User avatar
__philippe
Posts: 687
Joined: Wed Jun 26, 2013 2:09 am

Re: Listing and terminating processes from CLI

#8 Post by __philippe »

WinXP - frozen Explorer tidbit

For those rare but stubborn events where Win Explorer seems to be stuck solid or generally behaving oddly,
and last recourse appears to be the dreaded hard re-boot, try instead this mid-way, gentler fix:

• CTL-ALT-DEL to open Win Task Manager
• from 'Processes' Tab: locate, right-clik AND 'End Process' "explorer.exe"....poof :!:
:arrow: never fear...while still in TaskManager
• 'File' -> 'New Task (Run)' -> 'Open' -> type "explorer.exe" -> 'OK'

Should be happily back in business again...;-)

__philippe

User avatar
SYSTEM
Posts: 2041
Joined: Sat Jul 31, 2010 1:19 am
Location: Helsinki, Finland

Re: Listing and terminating processes from CLI

#9 Post by SYSTEM »

__philippe wrote:WinXP - frozen Explorer tidbit

For those rare but stubborn events where Win Explorer seems to be stuck solid or generally behaving oddly,
and last recourse appears to be the dreaded hard re-boot, try instead this mid-way, gentler fix:

• CTL-ALT-DEL to open Win Task Manager
• from 'Processes' Tab: locate, right-clik AND 'End Process' "explorer.exe"....poof :!:
:arrow: never fear...while still in TaskManager
• 'File' -> 'New Task (Run)' -> 'Open' -> type "explorer.exe" -> 'OK'

Should be happily back in business again...;-)

__philippe
I have restarted Explorer via Task Manager a few times (in Windows XP, 7 and 8). Mainly when something has terminated it without restarting it.
My YouTube channel | Release date of my 13th playlist: August 24, 2020

User avatar
__philippe
Posts: 687
Joined: Wed Jun 26, 2013 2:09 am

Re: Listing and terminating processes from CLI

#10 Post by __philippe »

Nice to know this trick works OK under Win7, for the inelucatble dark day is looming fast
when one finally has to relinquish old trusted XP for upgrading into Terra Incognita... :cry:
(End of XP support, April 2014, is that right ?)

"For there shall be much weeping and gnashing of teeth on that fateful day !" ... :wink:

__philippe

User avatar
Midas
Posts: 6710
Joined: Mon Dec 07, 2009 7:09 am
Location: Sol3

Re: SuperF4

#11 Post by Midas »

Thanks to Baas, I just found a XKill equivalent portable GUI tool for Windows, which even has a x64 version: SuperF4 (http://code.google.com/p/superf4/).
SuperF4 kills the foreground program when you press Ctrl+Alt+F4. This is different from when you press Alt+F4, which only asks the foreground program to exit, allowing it to decide for itself what to do. You can also kill a process by pressing Win+F4 and then clicking the window with your mouse. You can exit this mode without killing a program by pressing escape or the right mouse button.
Last edited by Midas on Sun Sep 15, 2013 8:40 am, edited 1 time in total.

User avatar
I am Baas
Posts: 4150
Joined: Thu Aug 07, 2008 4:51 am

Re: SuperF4

#12 Post by I am Baas »

Midas wrote:SuperF4 (http://code.google.com/p/superf4/).
Been submitted b4... viewtopic.php?t=9304 :P

User avatar
Midas
Posts: 6710
Joined: Mon Dec 07, 2009 7:09 am
Location: Sol3

Re: SuperF4

#13 Post by Midas »

I am Baas wrote:Been submitted b4... viewtopic.php?t=9304 :P
Great! (This time I didn't search... :)) Still relevant for the current topic.

User avatar
I am Baas
Posts: 4150
Joined: Thu Aug 07, 2008 4:51 am

Re: SuperF4

#14 Post by I am Baas »

Midas wrote:Still relevant for the current topic.
Absolutely. Also, check Quick Task Terminator.

User avatar
Midas
Posts: 6710
Joined: Mon Dec 07, 2009 7:09 am
Location: Sol3

Re: Listing and terminating processes from CLI

#15 Post by Midas »

Betanews review of payware AlomWare Reset (http://www.alomware.com/reset.htm) details an interesting (and somewhat reckless :)) use of TASKKILL -- among other CLI tools -- to emulate it...


Post Reply