Listing and terminating processes from CLI

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

Listing and terminating processes from CLI

#1 Post by Midas » Mon Mar 18, 2013 3:26 pm

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: 5223
Joined: Mon Dec 07, 2009 7:09 am
Location: Sol3

Re: Listing and terminating processes from CLI

#2 Post by Midas » Fri May 24, 2013 1:09 am

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: http://www.howtogeek.com/50630/batch-sc ... plication/

    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: 604
Joined: Wed Jun 26, 2013 2:09 am

Re: Listing and terminating processes from CLI

#3 Post by __philippe » Sun Jul 07, 2013 7:25 am

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: 5223
Joined: Mon Dec 07, 2009 7:09 am
Location: Sol3

Re: Listing and terminating processes from CLI

#4 Post by Midas » Sun Jul 07, 2013 10:40 am

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: 604
Joined: Wed Jun 26, 2013 2:09 am

Re: Listing and terminating processes from CLI

#5 Post by __philippe » Sun Jul 07, 2013 12:12 pm

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: 527
Joined: Sat Apr 08, 2006 7:12 pm
Location: Midwestern US

Re: Listing and terminating processes from CLI

#6 Post by TP109 » Sun Jul 28, 2013 8:15 pm

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: 5223
Joined: Mon Dec 07, 2009 7:09 am
Location: Sol3

Re: Listing and terminating processes from CLI

#7 Post by Midas » Mon Aug 05, 2013 7:25 am

Thanks for improving the topic, TP109. 8)

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

Re: Listing and terminating processes from CLI

#8 Post by __philippe » Tue Aug 06, 2013 6:10 am

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: 1899
Joined: Sat Jul 31, 2010 1:19 am
Location: Helsinki, Finland

Re: Listing and terminating processes from CLI

#9 Post by SYSTEM » Tue Aug 06, 2013 7:57 am

__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 12th playlist: November 1, 2018

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

Re: Listing and terminating processes from CLI

#10 Post by __philippe » Tue Aug 06, 2013 9:58 am

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: 5223
Joined: Mon Dec 07, 2009 7:09 am
Location: Sol3

Re: SuperF4

#11 Post by Midas » Sun Sep 15, 2013 8:34 am

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: 4146
Joined: Thu Aug 07, 2008 4:51 am

Re: SuperF4

#12 Post by I am Baas » Sun Sep 15, 2013 8:39 am

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

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

Re: SuperF4

#13 Post by Midas » Sun Sep 15, 2013 8:42 am

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: 4146
Joined: Thu Aug 07, 2008 4:51 am

Re: SuperF4

#14 Post by I am Baas » Sun Sep 15, 2013 11:03 pm

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

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

Re: Listing and terminating processes from CLI

#15 Post by Midas » Mon Jul 06, 2015 3:06 am

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