📄 400-402.html
字号:
<HTML>
<HEAD>
<TITLE>Special Edition Using Linux, Fourth Edition:Managing Multiple Processes</TITLE>
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!--ISBN=0789717468//-->
<!--TITLE=Special Edition Using Linux, Fourth Edition//-->
<!--AUTHOR=Jack Tackett//-->
<!--AUTHOR=Jr.//-->
<!--AUTHOR=Steve Burnett//-->
<!--PUBLISHER=Macmillan Computer Publishing//-->
<!--IMPRINT=Que//-->
<!--CHAPTER=19//-->
<!--PAGES=400-402//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="397-400.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="402-403.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>The entry in the second column of the <TT>ps</TT> listing is the PID of the process. In the following example, three processes are running for the current user (in addition to the shell). The current user’s name is pcoco.</P>
<!-- CODE SNIP //-->
<PRE>
$ <B>ps -ef | grep $LOGNAME</B>
pcoco 11805 11804 0 Dec 22 ttysb 0:01 sort sales.dat>sales.srt
pcoco 19955 19938 4 16:13:02 ttyp0 0:00 grep pcoco
pcoco 19938 1 0 16:11:04 ttyp0 0:00 bash
pcoco 19940 19938 142 16:11:04 ttyp0 0:33 find. -name core -exec rm {};
$
</PRE>
<!-- END CODE SNIP //-->
<P>To lower the priority on the process with PID 19940 (the <TT>find</TT> process), enter the following:</P>
<!-- CODE SNIP //-->
<PRE>
<B>renice -5 19940</B>
</PRE>
<!-- END CODE SNIP //-->
<P>As you would expect, the following statements are true about <TT>renice</TT>:</P>
<DL>
<DD><B>•</B> You can use <TT>renice</TT> only with processes you own.
<DD><B>•</B> The superuser can use <TT>renice</TT> on any process.
<DD><B>•</B> Only the superuser can increase the priority of a process.
</DL>
<H4 ALIGN="LEFT"><A NAME="Heading18"></A><FONT COLOR="#000077">Terminating Processes with <I>kill</I>
</FONT></H4>
<P>Sometimes, you want or need to terminate a process. The following are some reasons for stopping a process:
</P>
<DL>
<DD><B>•</B> It’s using too much CPU time.
<DD><B>•</B> It’s running too long without producing the expected output.
<DD><B>•</B> It’s producing too much output to the screen or to a disk file.
<DD><B>•</B> It appears to have locked a terminal or some other session.
<DD><B>•</B> It’s using the wrong files for input or output because of an operator or programming error.
<DD><B>•</B> It’s no longer useful.
</DL>
<P>Most likely, you’ll come across a number of other reasons to kill a process as well. If the process to be stopped is a background process, use the <TT>kill</TT> command to get out of these situations.</P>
<P>To stop a command that isn’t in the background, press <Ctrl-c>. When a command is in the background, however, pressing an interrupt key doesn’t stop it. Because a background process isn’t under terminal control, keyboard input of any interrupt key is ignored. The only way you can stop background commands is to use the <TT>kill</TT> command.</P>
<P><FONT SIZE="+1"><B>Normal Termination of Background Processes</B></FONT></P>
<P>The <TT>kill</TT> command sends signals to the program to demand that a process be terminated or killed. To use <TT>kill</TT>, use either of these forms:</P>
<!-- CODE SNIP //-->
<PRE>
kill PID(s)
</PRE>
<!-- END CODE SNIP //-->
<P>or
</P>
<!-- CODE SNIP //-->
<PRE>
kill -<I>signal PID</I>(<I>s</I>)
</PRE>
<!-- END CODE SNIP //-->
<P>To kill a process whose PID is 123, enter <TT><B>kill 123</B></TT>. To kill several processes whose PIDs are 123, 342, and 73, enter <TT><B>kill 123 342 73</B></TT>.</P>
<P>By using the <I><TT>-signal</TT></I> option, you can do more than simply kill a process. Other signals can cause a running process to reread configuration files or stop a process without killing it. Valid signals are listed by the command <TT>kill -l</TT>. An average user, however, will probably use <TT>kill</TT> with no signal or, at most, with the -9 signal (the I-mean-it-so-don’t-ignore-me signal, described in the next section).</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>CAUTION: </B><BR>Use the correct PID with the <TT>kill</TT> command. Using the wrong PID can stop a process you want to keep running. Remember that killing the wrong process or a system process can have disastrous effects. Also remember that if you’re logged in as the system administrator, you can kill <TT>any</TT> process.<HR></FONT>
</BLOCKQUOTE>
<P>If you successfully kill the process, you get no notice from the shell; the shell prompt simply reappears. You see an error message if you try to kill a process you don’t have permission to kill or if you try to kill a process that doesn’t exist.
</P>
<P>Suppose that your login name is chris and that you’re now logged in to tty01. To see the processes you have running, enter <TT><B>ps -f</B></TT>, and you’ll see the following response:</P>
<!-- CODE SNIP //-->
<PRE>
UID PID PPID C STIME TTY TIME COMMAND
chris 65 1 0 11:40:11 tty01 0:06 -bash
chris 71 65 61 11:42:01 tty01 0:14 total_updt
chris 231 65 80 11:46:02 tty01 0:00 ps -f
chris 187 53 60 15:32:01 tty02 123:45 crunch stats
chris 53 1 0 15:31:34 tty02 1:06 -bash
</PRE>
<!-- END CODE SNIP //-->
<P>Notice that the program <TT>total_updt</TT> is running at your current terminal. Another program, <TT>crunch</TT>, is running on another terminal, and you think it has used an unusually large amount of CPU time. To kill that process, it may be sufficient to enter <TT><B>kill 187</B></TT>. To kill the parent of that process, enter <TT><B>kill 53</B></TT>.</P>
<P>You may want to kill a parent and its child if you logged in as the system administrator and see that someone left their terminal unattended (if you’ve set up Linux with remote terminals). You can kill a clock process that the user has running (the child process) and the login shell (the parent process) so that the unattended terminal is no longer logged in.</P>
<P>Stopping the parent of a process sometimes terminates the child process as well. To be sure, stop the parent and its children to halt all activity associated with a parent process. In the preceding example, enter <TT><B>kill 187 53</B></TT> to terminate both processes.</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>TIP: </B>If your terminal locks up, log in to another virtual terminal by pressing <Alt> combined with a function key (F1–F6), enter <TT><B>ps -ef | grep $LOGNAME</B></TT>, and then kill the login shell for the locked terminal.<HR></FONT>
</BLOCKQUOTE>
<P><FONT SIZE="+1"><B>Unconditional Termination of Background Processes</B></FONT></P>
<P>Issuing the <TT>kill</TT> command sends a signal to a process. Linux programs can send or receive more than 20 signals, each of which is represented by a number. For example, when you log out, Linux sends the hang-up signal (signal number 1) to all the background processes started from your login shell. This signal kills or stops those processes unless they were started with <TT>nohup</TT> (as described earlier in this chapter).</P>
<P>Using <TT>nohup</TT> to start a background process lets the process ignore the signal that tries to stop it. You may be using programs or shell scripts written to ignore some signals. If you don’t specify a signal when you use <TT>kill</TT>, signal 15 is sent to the process. The command <TT>kill 1234</TT> sends signal 15 to the process whose PID is 1234. If that process is set to ignore signal 15, however, the process doesn’t terminate when you use this command. You can use <TT>kill</TT> in a way that a process “can’t refuse,” however.</P>
<P>The signal 9 is an unconditional kill signal; it always kills a process. To unconditionally kill a process, use the following command:</P>
<!-- CODE SNIP //-->
<PRE>
<B>kill -9</B> PID
</PRE>
<!-- END CODE SNIP //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="397-400.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="402-403.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -