Page 1 of 1

Listing and terminating processes from CLI

Posted: Mon Mar 18, 2013 3:26 pm
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.

Re: Listing and terminating processes from CLI

Posted: Fri May 24, 2013 1:09 am
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

Re: Listing and terminating processes from CLI

Posted: Sun Jul 07, 2013 7:25 am
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

Re: Listing and terminating processes from CLI

Posted: Sun Jul 07, 2013 10:40 am
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 ... ;)

Re: Listing and terminating processes from CLI

Posted: Sun Jul 07, 2013 12:12 pm
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

Re: Listing and terminating processes from CLI

Posted: Sun Jul 28, 2013 8:15 pm
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.

Re: Listing and terminating processes from CLI

Posted: Mon Aug 05, 2013 7:25 am
by Midas
Thanks for improving the topic, TP109. 8)

Re: Listing and terminating processes from CLI

Posted: Tue Aug 06, 2013 6:10 am
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

Re: Listing and terminating processes from CLI

Posted: Tue Aug 06, 2013 7:57 am
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.

Re: Listing and terminating processes from CLI

Posted: Tue Aug 06, 2013 9:58 am
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

Re: SuperF4

Posted: Sun Sep 15, 2013 8:34 am
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.

Re: SuperF4

Posted: Sun Sep 15, 2013 8:39 am
by I am Baas
Midas wrote:SuperF4 (http://code.google.com/p/superf4/).
Been submitted b4... viewtopic.php?t=9304 :P

Re: SuperF4

Posted: Sun Sep 15, 2013 8:42 am
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.

Re: SuperF4

Posted: Sun Sep 15, 2013 11:03 pm
by I am Baas
Midas wrote:Still relevant for the current topic.
Absolutely. Also, check Quick Task Terminator.

Re: Listing and terminating processes from CLI

Posted: Mon Jul 06, 2015 3:06 am
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...