Полезная информация

Learning the Unix Operating System

Learning the Unix Operating SystemSearch this book
Previous: 6.2 Checking on a ProcessChapter 6
Multitasking
Next: 7. Where to Go from Here
 

6.3 Cancelling a Process

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

6.3.1 kill

The kill command aborts a process. The 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 number of seconds. We enter two commands, sleep and who, on the same line, as a background process.

% (sleep 60; who)&
[1] 21087
% ps
 PID    TTY   TIME  COMMAND
20055    4    0:10  sh
21087    4    0:01  sh
21088    4    0:00  sleep
21089    4    0:02  ps
% kill 21088
Terminated
% tom     tty2   Aug 30  11:27
grace   tty4   Aug 30  12:24
tim     tty5   Aug 30  07:52
dale    tty7   Aug 30  14:34

We decided that 60 seconds was too long a time to wait for the output of who. The ps listing showed that sleep had the process ID number 21088, so we used 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.

The who command is executed immediately, since it is no longer waiting on sleep; it lists the users logged into the system.

6.3.2 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 (like 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 will generally kill any processes receiving that data.


Previous: 6.2 Checking on a ProcessLearning the Unix Operating SystemNext: 7. Where to Go from Here
6.2 Checking on a ProcessBook Index7. Where to Go from Here

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System