Jon Aquino's Mental Garden

Engineering beautiful software jon aquino labs | personal blog

Sunday, February 19, 2006

Kill any program/process with a few keystrokes

Outlook dies a lot, and often not completely -- you need to open up Task Manager, look for the OUTLOOK.EXE process, and kill it. I'm going to show you how to set up your system to kill it by typing "Win+R kil outlook". Actually you can use this to kill any process e.g. "Win+R kil firefox"

1. Install Cygwin. This will add several unix-like commands to your computer.
2. Open Notepad and make a text file called kil.bat containing the following line:

ps -W | grep --ignore-case %1 | gawk '{print $1}' | xargs --replace=_ kill -f _

And you're done. Now you can bring up the Run dialog with Win+R and type kil outlook (or whatever) to kill that process.

I've been using the Run dialog more and more since I read about the Lifehacker Run Dialog technique. Here are some other interesting Run dialog shortcuts I made:
  • ff to open a new Firefox window - shortcut to C:\Program Files\Mozilla Firefox\firefox.exe - very handy as I like having multiple Firefox windows
  • task to open the Task Manager - shortcut to C:\WINDOWS\system32\taskmgr.exe
  • add to open the Add/Remove control panel - right-click the control panel and select Create Shortcut
  • cal to open Outlook's calendar view - "C:\Program Files\Microsoft Office\Office10\OUTLOOK.EXE" /select outlook:calendar
  • pblog to open an Outlook email template pre-filled with my blog-by-email address - "C:\Run\pblog.oft"
  • pai to open an AutoHotKey macro that opens Paint, clicks red, clicks the medium line thickness, then clicks the circle tool - C:\Run\pai.ahk
  • run to open C:\Run for adding more of these shortcuts
  • shut to shutdown the computer - batch file containing: shutdown -r -t 0

23 Comments:

  • Genius. Thanks Jon. I get really hacked off with Outlook failing to go quietly. I also have one or two Outlook add-ons which hang around long after they should have left the party.
    This is very handy.

    By Blogger Neil MacLean, at 2/19/2006 2:34 p.m.  

  • Neil - I'm thrilled to hear that you find it useful. Yay!

    By Blogger Jonathan, at 2/19/2006 2:34 p.m.  

  • Another option if you are using WinXP, create a file called kill.bat with the following contents:

    taskkill /F /IM %1 /T

    Then save that file somewhere within your system path. Running Kill <process name> will forcebly kill that process and any children processes it may have.

    By Anonymous Anonymous, at 2/19/2006 3:00 p.m.  

  • Nice one anon - I didn't know about taskkill. People don't have to install cygwin if they use that (note: I haven't tried it yet).

    By Blogger Jonathan, at 2/19/2006 3:06 p.m.  

  • One update -- to get it to emulate the same functionality, you can make it:

    taskkill /F /IM %1* /T

    That way you could use kill notepad and not need to put in the .exe as it will do wildcard partial matches

    By Anonymous Anonymous, at 2/19/2006 3:10 p.m.  

  • taskkill is a nice and undocumented feature of Windows.

    But I still prefer the Cygwin approach. You can do much more with Cygwin, and if you take the time to learn it, you end up with a library of very useful one-line automations.

    Ctrl+Shift+Esc brings up the taskmanager. I use Process Explorer from SysInternals, but I think it's the default for the built in taskmanager.

    By Anonymous Anonymous, at 2/19/2006 8:12 p.m.  

  • Hi assaf - thanks - I feel a bit better about the cygwin solution :-)

    By Blogger Jonathan, at 2/19/2006 8:28 p.m.  

  • I love cygwin as well for all the other capabilities, wasn't trying to say his suggestion wasn't good, just wanted to offer up another alternative :)

    By Anonymous Anonymous, at 2/20/2006 6:24 a.m.  

  • Here's an idea:

    1. Create the batch file using the following code, it will prompt you to either enter the name of the process to be killed or "end" to exit. It uses the taskkill function but takes you down to just having to type the name of the app (how lazy can we get?).

    set /p app=Please enter the name of the app to close or type END.
    if /i [%app%]==[END] endlocal&goto end
    taskkill /F /IM %app%* /T
    exit
    :end
    exit

    Next, put the batch file in your start menu, open your start menu, navigate to the file, right-click it, select properties, click in the "shortcut key" blank, and then press a combination of keys to assign a shortcut to the batch file.

    Now all you have to do is press the key combination you selected and type the name of the app. So we've replaced Start+R with the key combination of your choice (I'm thinking Start+K?) and saved you from having to type the name of the batch file.

    You can use the properties dialogue from the windows start menu to create super-fast keystroke shortcuts to any app or batch file, very handy with windows/dos or cygwin flavored batch files. Want to open up another firefox window? Find firefox in your start menu and create a keyboard-based shortcut for it using the properties dialogue.

    By Anonymous Anonymous, at 2/20/2006 2:17 p.m.  

  • P.S.
    You may want to start the above code off with an:

    @echo off

    to clean things up a bit.

    JP

    By Anonymous Anonymous, at 2/20/2006 2:25 p.m.  

  • Hi JP - thanks for the tip!

    By Blogger Jonathan, at 2/20/2006 10:09 p.m.  

  • Amusing, how many ways there are to bring up the task manager. Personally I prefer ctrl+alt+delete.

    By Blogger Johan Sundström, at 2/24/2006 5:36 p.m.  

  • Hi Johan - Hm! I thought Ctrl+Alt+Delete wouldn't bring up the task manager directly - guess I must be thinking of Win2000. Great tip for XP!

    By Blogger Jonathan, at 2/24/2006 11:24 p.m.  

  • THANK YOU anonymous, and jp!

    By Anonymous Anonymous, at 12/14/2006 7:08 a.m.  

  • Hi,
    Can you give a script to kill a prenamed application..
    for example if i want to kill only
    notepad..
    and call my batch script killnotepad.bat..

    how do i do it effectively ?
    ps -W | grep --ignore-case "notepad" | gawk '{print $1}' | xargs --replace=_ kill -f _

    By Anonymous Anonymous, at 4/10/2007 8:32 a.m.  

  • Hi Anon - I haven't tried this, but based on a comment above you might be able to do:

    taskkill /F /IM notepad.exe /T

    By Blogger Jonathan, at 4/10/2007 9:55 p.m.  

  • Hi, Jon, nice knowing there are others out there with the same passion for Cygwin, I haven't stopped using it since 2002, nice one liner for killing processes

    By Anonymous Anonymous, at 6/07/2007 4:41 p.m.  

  • hey jC - yup, Cygwin rocks. Learned about it from the excellent book "The Pragmatic Programmer".

    By Blogger Jonathan, at 6/07/2007 8:11 p.m.  

  • %windir%\System32\TASKKILL /F /FI "IMAGENAME eq firefox.exe"

    kills firefox

    By Anonymous Anonymous, at 12/28/2007 3:48 p.m.  

  • Thanks Anon - I am now using taskkill - works great.

    By Blogger Jonathan, at 12/28/2007 6:44 p.m.  

  • I am using Windows XP Pro with the latest Cygwin.

    Your script of "ps -W | grep --ignore-case %1 | gawk '{print $1}' | xargs --replace=_ kill -f _"

    would not work over and over, until I changed the "grep --ignore-case %1" to "grep --ignore-case $1".

    By Blogger RedPenguin, at 12/20/2009 9:29 p.m.  

  • Nevermind, it seems if you run it inside Cygwin it self, it wants $1.

    Yet if you run from the Run command window, then it works only with %1.

    Must be Linux wants $1 while Windows wants %1.

    By Blogger RedPenguin, at 12/20/2009 9:45 p.m.  

  • Gr8 man.. this command helped me a lot!

    By Anonymous Anonymous, at 2/23/2012 4:02 p.m.  

Post a Comment

<< Home