Canceling a Process

You may decide that you shouldn’t have put a process in the background or the process is taking too long to execute. You can cancel a background process if you know its process ID.

Tip

Mac OS X includes a very helpful utility called Force Quit, accessible from the Apple menu, which can be quite useful when applications are stuck or nonresponsive. However, commands entered into the Terminal window can only be cancelled from the command line—they don’t show up in Force Quit. In addition, Force Quit doesn’t show you administrative processes. To stop Unix programs and administrative processes, you must use the command line or the Activity Monitor.

kill

The kill command terminates a process. This has the same result as using the Finder’s Force Quit command. The kill command’s format is:

kill PID(s)

kill terminates the designated process IDs (shown under the PID heading in the ps listing). If you do not know the process ID, do a ps first to display the status of your processes.

In the following example, the sleep n command simply causes a process to “go to sleep” for n seconds. We enter two commands, sleep and who, on the same line, as a background process.

$ (sleep 60;who) &
[1] 472
$ ps
  PID  TT  STAT      TIME COMMAND
  347 std  S      0:00.36 -bash
  472 std  S      0:00.00 -bash
  473 std  S      0:00.01 sleep 60
$ kill 473
$ -bash: line 53:   473 Terminated              sleep 60
taylor   console  Sep 24 22:38 
taylor   ttyp1    Sep 24 22:40 

[1]+  Done                    ( sleep 60; who )
$

We decided that 60 seconds was too long to wait ...

Get Learning Unix for Mac OS X Panther now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.