Learning Unix for Mac OS X Panther

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.

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.

7.3.1 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 for the output of who . The ps listing showed that sleep had the process ID number 472, so we use this PID to kill the sleep process. You should see a message like "terminated" or " killed "; if you don't, use another ps command to be sure the process has been killed.

In our example, the who program is now executed immediately, as it is no longer waiting on sleep ; it lists the users logged into the system.

7.3.1.1 Problem checklist

The process didn't die when I told it to.

Some processes can be hard to kill. If a normal kill of these processes is not working, enter kill -9 PID . This is a sure kill and can destroy almost anything, including the shell that is interpreting it.

In addition, if you've run an interpreted program (such as a shell script), you may not be able to kill all dependent processes by killing the interpreter process that got it all started; you may need to kill them individually. However, killing a process that is feeding data into a pipe generally kills any processes receiving that data.

Категории