📄 ch38_10.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 38] 38.10 Destroying Processes with kill </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly & Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:49:46Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-260-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch38_01.htm"TITLE="38. Starting, Stopping, and Killing Processes"><LINKREL="prev"HREF="ch38_09.htm"TITLE="38.9 Killing Foreground Jobs "><LINKREL="next"HREF="ch38_11.htm"TITLE="38.11 Printer Queue Watcher: A Restartable Daemon Shell Script"></HEAD><BODYBGCOLOR="#FFFFFF"TEXT="#000000"><DIVCLASS="htmlnav"><H1><IMGSRC="gifs/smbanner.gif"ALT="UNIX Power Tools"USEMAP="#srchmap"BORDER="0"></H1><MAPNAME="srchmap"><AREASHAPE="RECT"COORDS="0,0,466,58"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="467,0,514,18"HREF="jobjects/fsearch.htm"ALT="Search this book"></MAP><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch38_09.htm"TITLE="38.9 Killing Foreground Jobs "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 38.9 Killing Foreground Jobs "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 38<BR>Starting, Stopping, and Killing Processes</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch38_11.htm"TITLE="38.11 Printer Queue Watcher: A Restartable Daemon Shell Script"><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 38.11 Printer Queue Watcher: A Restartable Daemon Shell Script"BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-6120">38.10 Destroying Processes with kill </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-43009"></A><ACLASS="indexterm"NAME="AUTOID-43012"></A><ACLASS="indexterm"NAME="AUTOID-43015"></A>Sometimes it's necessary to eliminate a process entirely or to<SPANCLASS="link">signal a process (<ACLASS="linkend"HREF="ch38_11.htm"TITLE="Printer Queue Watcher: A Restartable Daemon Shell Script">38.11</A>)</SPAN>;this is the purposeof the <EMCLASS="emphasis">kill</EM> command. You can use the <EMCLASS="emphasis">kill</EM> command with orwithout a signal id:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>kill</B></CODE> <CODECLASS="replaceable"><I>pid</I></CODE>% <CODECLASS="userinput"><B>kill</B></CODE> <CODECLASS="replaceable"><I>-signal pid</I></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">where <EMCLASS="emphasis">pid</EM> is the process' identification number, and <EMCLASS="emphasis">signal</EM> (which is optional) isthe signal to send to the process. The default signal isnumber 15, the <EMCLASS="emphasis">TERM</EM> signal, which tells the process to terminate. UnderSystem V, the signal must be specified numerically; under BSD, either thesignal number or its symbolic name may be used.[Use <EMCLASS="emphasis">kill -l</EM> for a list of signal names;unfortunately, the listing doesn't show the correspondence of namesand numbers. However, they are in order, so if you can count, youcan figure it out. <EMCLASS="emphasis">-TOR</EM> ]</P><PCLASS="para">Sometimes, a process may still exist after a <EMCLASS="emphasis">kill</EM> command. If thishappens, execute the <EMCLASS="emphasis">kill</EM> command with the <EMCLASS="emphasis">-9</EM> option, whichsends theprocess signal 9, appropriately named <EMCLASS="emphasis">KILL</EM>. This almost always guaranteesthat the process will be destroyed. However, it does not allow the dyingprocess to clean up, and therefore may leave the process' files in aninconsistent state. </P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-43038"></A>Occasionally, processes will not die even after being sent the<EMCLASS="emphasis"> KILL</EM> signal.The vast majority of such processes fall into one of three categories:</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-43044"></A><ACLASS="indexterm"NAME="AUTOID-43046"></A>Zombies.A process in the<SPANCLASS="link">zombie state (<ACLASS="linkend"HREF="ch38_16.htm"TITLE="Why You Can't Kill a Zombie ">38.16</A>)</SPAN><SPANCLASS="link"> is displayed as <EMCLASS="emphasis">Z</EM> status in BSD<EMCLASS="emphasis">ps</EM> displays and as <EMCLASS="emphasis"><defunct></EM> under System V . (<ACLASS="linkend"HREF="ch38_05.htm"TITLE="The ps Command ">38.5</A>)</SPAN>When a process isexiting, it informs its parent of its imminent death; when it receives anacknowledgment, its PID is removed from the process table.A zombie processis one whose total resources have been freed, but the parent process'acknowledgment has not occurred. Usually, <EMCLASS="emphasis">init</EM> will step in when theparent is gone, but very occasionally this fails to happen. Zombiesare always cleared the next time the system is booted and do not adverselyaffect system performance.</P></LI><LICLASS="listitem"><PCLASS="para">Processes waiting for unavailable<SPANCLASS="link">NFS (<ACLASS="linkend"HREF="ch01_33.htm"TITLE="UNIX Networking and Communications ">1.33</A>)</SPAN>resources (for example, trying towrite to a remote file on a system that has crashed) will not die if sent a<EMCLASS="emphasis">KILL</EM> signal.Use the <EMCLASS="emphasis">QUIT</EM> signal (3) or the <EMCLASS="emphasis">INT</EM> (interrupt) signal (2)to kill such processes.</P></LI><LICLASS="listitem"><PCLASS="para">Processes waiting for a device to complete an operation before exiting.Often this means waiting for a tape to finish rewinding.</P></LI></UL><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-43063"></A><ACLASS="indexterm"NAME="AUTOID-43066"></A>Killing a process may also kill all of its children.Child processes may not die if they're blocking or "catching" the signal youuse-although, as explained above, the <EMCLASS="emphasis">KILL</EM> signal (9) will usuallyterminate those processes.Killing a shell cantherefore kill all the foreground and stopped background processesinitiated from that shell (including other shells). Killing a user's loginshell is equivalent to logging the user out. This is a useful (if somewhatpainful) way to recover from certain kinds of problems. For example, if auser manages to confuse his editor by mistyping control keys and escapesequences, or enters an infinite loop that he can't terminate by normalmeans, killing his shell will let him regain control of the situation,possibly at the cost of some work. Use the<EMCLASS="emphasis">ps</EM> commandto determine which process is the offending user's shell. Remember that youmust be<SPANCLASS="link">superuser (<ACLASS="linkend"HREF="ch01_24.htm"TITLE="The Superuser (Root) ">1.24</A>)</SPAN>in order to kill someone else's process.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">AF</SPAN> <SPANCLASS="bibliomisc">from O'Reilly & Associates' <CITECLASS="citetitle">Essential System Administration</CITE>, Chapter 7</SPAN></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch38_09.htm"TITLE="38.9 Killing Foreground Jobs "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 38.9 Killing Foreground Jobs "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="book"HREF="index.htm"TITLE="UNIX Power Tools"><IMGSRC="gifs/txthome.gif"SRC="gifs/txthome.gif"ALT="UNIX Power Tools"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch38_11.htm"TITLE="38.11 Printer Queue Watcher: A Restartable Daemon Shell Script"><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 38.11 Printer Queue Watcher: A Restartable Daemon Shell Script"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">38.9 Killing Foreground Jobs </TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="index"HREF="index/idx_0.htm"TITLE="Book Index"><IMGSRC="gifs/index.gif"SRC="gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172">38.11 Printer Queue Watcher: A Restartable Daemon Shell Script</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><IMGSRC="gifs/smnavbar.gif"SRC="gifs/smnavbar.gif"USEMAP="#map"BORDER="0"ALT="The UNIX CD Bookshelf Navigation"><MAPNAME="map"><AREASHAPE="RECT"COORDS="0,0,73,21"HREF="../index.htm"ALT="The UNIX CD Bookshelf"><AREASHAPE="RECT"COORDS="74,0,163,21"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="164,0,257,21"HREF="../unixnut/index.htm"ALT="UNIX in a Nutshell"><AREASHAPE="RECT"COORDS="258,0,321,21"HREF="../vi/index.htm"ALT="Learning the vi Editor"><AREASHAPE="RECT"COORDS="322,0,378,21"HREF="../sedawk/index.htm"ALT="sed & awk"><AREASHAPE="RECT"COORDS="379,0,438,21"HREF="../ksh/index.htm"ALT="Learning the Korn Shell"><AREASHAPE="RECT"COORDS="439,0,514,21"HREF="../lrnunix/index.htm"ALT="Learning the UNIX Operating System"></MAP></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -