How to kill a process via the Terminal

NewTerminalIconXWhen processes and applications hang in OS X, sometimes the only way to re-gain functionality is to force-quit them. For applications like Word or Safari, perhaps the simplest approach is to use Apple’s force-quit window, which can be invoked from the Apple menu, or by pressing the Option-Command-Escape hotkey. For background processes, scripts, and commands that generally run in the background, you will have to use alternative means.

One such option is to use Apple’s Activity Monitor program to view all running processes and identify ones that are hung up, and then select and force-quit them there; however, you can also use the Terminal to do this, if needed.

There are three commands that you can use to kill any process in the Terminal, using either its process ID (PID) or its name.

kill

If you know the ID number of the specific process you would like to kill, then you can simply type “kill PID” or “sudo kill PID” to target this process and terminate it. This might be necessary as part of troubleshooting if you see this process ID listed with errors in the console or in other areas where you are monitoring the background activity of your Mac.

killall

If you do not know the ID number of the specific process you would like to kill, then you can use its process name with the “killall” command. For example, if your Dock is hanging so when you hover your mouse over it you see the spinning color wheel cursor, you can kill and relaunch the Dock by typing “killall Dock” in the Terminal.

pkill

TerminalKillAll

The “killall” command can be used to specify a process by name, instead of needing to know its process ID.

While the “kill” and “killall” commands should suffice for most purposes where you want to force-quit a process, another way to kill a process is to use “pkill,” the benefit here being you can kill processes using partial names and wildcard characters, instead of having to specify the entire name. This may be particularly useful if you have a program running that has various satellite processes and helper applications launched, which are all similarly named. For example, if you want to quit the QTKitServer process that runs as a plugin for Safari, this will launch in numerous instances with unique IDs appended to each instance name; however, they will all begin with the name “QTKitServerā€¦” To kill all of these, you simply need to run “pkill QTKitServer*” and they will all be shut down.

Note that while these approaches can be useful, the killing of any process in OS X is unforgiving. While in many cases process activity will be governed by a launch daemon or agent that will simply re-start the hung process, killing a process may result in unwanted and unpredictable system behavior. Granted if this is already occurring then you can interrupt it by killing the process, but overall it is a relatively crude approach to solving problems.

If these processes continue to hang and show unwanted behavior, especially after restarting or logging out and back into your accocunt, then you will need to look into other approaches for solving the issue at hand.

4 thoughts on “How to kill a process via the Terminal

  1. darkdreamer4u

    I often use the dock to force quit an application: simply click and hold the offending application’s icon in the dock and a menu with various options pops up, one of which is to force quit the application.

    Reply
    1. Strod

      Right-clicking and Control-clicking on the Dock icon will also display the contextual menu with the Force Quit entry.
      If the application isn’t really hung, you will see a “Quit” option instead of “Force Quit”. That makes it a handy way to quit apps nicely, since you don’t need to switch to the app first.
      But sometimes you want to force quit an app that the system doesn’t believe is hung, and choosing the “Quit” entry does not do the trick. (Example: an app stuck in a genuine loop or some other long-processing task that you no longer want to wait for completion.) In that case, you can invoke the Force Quit item by pressing the option key while the contextual menu is displayed.

      Reply
  2. Strod

    One comment on the kill command: kill PID by itself doesn’t really kill the process with that PID. What it does is send it a termination signal. Normally, a well behaved program will catch the signal and die peacefully.

    But sometimes the process is trapped in a state that does not allow it to reach the point in which it would catch signals sent by the OS. In that case, you will need to tell the system to kill the process directly instead of asking it to die. That can be accomplished by issuing a “non-catchable, non-ignorable kill”:
    kill -9 PID

    As a rule of thumb it is always a good idea to try to kill the process “nicely” because some processes will be nice enough to perform some cleanup before committing seppuku. Only if this fails (i.e., the zombie refuses to die), go ahead and send it a kill -9 .

    Reply
  3. Aram Fingal

    The really nice way to quit a process from the terminal is to use the built-in AppleScript interpreter osascript litke this:

    osascript -e “tell application \”GraphicConverter\” to quit saving yes”

    Of course it only works on scriptable applications but it will cause them to save any unsaved documents first. One situation where this is a really good thing to do is when the Mac GUI freezes but you can still get in by ssh. In fact this is the first thing to do in such a case. Then you can try force quitting things. Sometimes you can unfreeze the Mac that way, most often (when it works) it’s force quitting, with kill -9, a WindowServer process that will do the trick. More often, I end up using sudo shutdown -r now, which is, at least, better than holding down the power button.

    By the way, I followed MacFixIt from the beginning but drifted away a while after it moved to C|net. I just now got wind of this site and I’m glad the niche is filled again. Topher was always good. It’s C|net I didn’t like.

    Reply

Leave a Reply to Strod Cancel reply