📄 394-397.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=394-397//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="392-394.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="397-400.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>The <TT>ps</TT> listing displays four default headings as indicators of the information in the fields below each heading: <TT>PID</TT>, <TT>TTY</TT>, <TT>TIME</TT>, and <TT>COMMAND</TT>. Table 19.6 explains these headings.</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 19.6</B> Headings in the Output of <TT>ps</TT>
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="15%" ALIGN="LEFT">Field
<TH WIDTH="85%" ALIGN="LEFT">Explanation
<TR>
<TD COLSPAN="2"><HR>
<TR>
<TD><TT>PID</TT>
<TD>The process identification number
<TR>
<TD><TT>TTY</TT>
<TD>The terminal on which the process originated
<TR>
<TD><TT>TIME</TT>
<TD>The cumulative execution time for the process, in minutes and seconds
<TR>
<TD><TT>COMMAND</TT>
<TD>The name of the command being executed
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<P>Suppose that you want to sort a file named sales.dat, save a copy of the sorted file in a file named sales.srt, and mail the sorted file to the user sarah. If you also want to put this job in the background, enter the following command:
</P>
<!-- CODE SNIP //-->
<PRE>
<B>sort sales.dat | tee sales.srt | mailx -s“Sorted Sales Data” sarah &</B>
</PRE>
<!-- END CODE SNIP //-->
<P>To monitor this process, enter <B><TT>ps</TT></B> to see a display such as this one:</P>
<!-- CODE SNIP //-->
<PRE>
PID TTY TIME COMMAND
16490 tty02 0:15 sort
16489 tty02 0:00 mailx
16492 tty02 0:00 ps
16478 tty02 0:00 bash
16491 tty02 0:06 tee
16480 tty02 96:45 cruncher
</PRE>
<!-- END CODE SNIP //-->
<P>You see the accumulated time and PID for each process started with the command. You also see information for your login shell (<TT>bash</TT>) and for <TT>ps</TT> itself. Notice that all the commands in the pipe are running at once, just as you would expect (this is the way the piping process works). The last entry is for a command that has been running for more than an hour and a half. If that’s a problem, you may want to terminate the process by using the <TT>kill</TT> command (described later in this chapter). If you enter <TT>ps</TT> and see only the following listing, the previous job you put into the background is complete:</P>
<!-- CODE SNIP //-->
<PRE>
PID TTY TIME COMMAND
16492 tty02 0:00 ps
16478 tty02 0:00 bash
16480 tty02 99:45 cruncher
</PRE>
<!-- END CODE SNIP //-->
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>NOTE: </B>Use <TT>ps</TT> occasionally to check the status of a command. If, however, you use <TT>ps</TT> every second while waiting to see whether the background job is complete, putting the job in the background doesn’t make much sense in the first place.<HR></FONT>
</BLOCKQUOTE>
<P><FONT SIZE="+1"><B>Obtaining More Information About Processes with <I>ps</I>
</B></FONT></P>
<P>Sometimes you need to know more about your processes than what the default <TT>ps</TT> listing provides. To generate additional information, you can invoke some of the flags listed in Table 19.7.</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 19.7</B> Commonly Used Flags for the <TT>ps</TT> Command
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="15%" ALIGN="LEFT">Flag
<TH WIDTH="85%" ALIGN="LEFT">Description
<TR>
<TD COLSPAN="2"><HR>
<TR>
<TD><TT>-a</TT>
<TD>Shows processes of other users also.
<TR>
<TD><TT>-c</TT>
<TD>Displays command name from <TT>task_struct</TT> environment.
<TR>
<TD><TT>-e</TT>
<TD>Shows environment after command line and “and.”
<TR>
<TD><TT>-f</TT>
<TD>Shows “forest” family tree format (processes and subprocesses).
<TR>
<TD><TT>-h</TT>
<TD>No header.
<TR>
<TD><TT>-j</TT>
<TD>Jobs format.
<TR>
<TD><TT>-l</TT>
<TD>Long format.
<TR>
<TD><TT>-m</TT>
<TD>Displays memory info.
<TR>
<TD VALIGN="TOP"><TT>-n</TT>
<TD>Numeric output for <TT>USER</TT> and <TT>WCHAN</TT>. <TT>WCHAN</TT> is the name of the kernel function where the process is sleeping, with the <TT>sys_</TT> stripped from the function name. If /etc/psdatabase doesn’t exist, the number is hexadecimal instead.
<TR>
<TD><TT>-r</TT>
<TD>Running processes only.
<TR>
<TD><TT>-s</TT>
<TD>Signal format.
<TR>
<TD><TT>-S</TT>
<TD>Adds child CPU time and page faults.
<TR>
<TD><TT>-t<I>xx</I></TT>
<TD>Processes associated with tty<I>xx</I> only.
<TR>
<TD><TT>-u</TT>
<TD>User format; gives user name and start time.
<TR>
<TD><TT>-v</TT>
<TD>vm (virtual memory) format.
<TR>
<TD><TT>-w</TT>
<TD>Wide output; doesn’t truncate command lines to fit on one line.
<TR>
<TD><TT>-x</TT>
<TD>Shows processes without controlling terminal.
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<P>The <TT>ps</TT> command gives only an approximate picture of process status because things can and do change while the <TT>ps</TT> command is running. The <TT>ps</TT> command gives a snapshot of the process status at the instant <TT>ps</TT> executed. The snapshot includes the <TT>ps</TT> command itself.</P>
<P>The following examples show three commands. The first command is the login shell (<TT>bash</TT>). The second command is <TT>sort</TT>, which is used to sort the file named inventory. The third command is the <TT>ps</TT> command you’re now running.</P>
<P>To find out what processes you’re now running, use the following command:</P>
<!-- CODE SNIP //-->
<PRE>
$ <B>ps</B>
PID TTY TIME COMMAND
65 tty01 0:07 -bash
71 tty01 0:14 sort inventory
231 tty01 0:09 ps
</PRE>
<!-- END CODE SNIP //-->
<P>To obtain a full listing, use this command:
</P>
<!-- CODE SNIP //-->
<PRE>
$ <B>ps -uax</B>
UID PID PPID C STIME TTY TIME COMD
amanda 65 1 0 11:40:11 tty01 0:06 -bash
amanda 71 65 61 11:42:01 tty01 0:14 sort inventory
amanda 231 65 80 11:46:02 tty01 0:00 ps -f
</PRE>
<!-- END CODE SNIP //-->
<P>Notice a few things about this full listing. In addition to the PID, the PPID is listed. The PPID is the process ID number of that process’s parent process. In this example, the first process listed, PID 65, is the parent of the following two. The entry in the fourth column (the column headed C) gives the amount of CPU time a process has used recently. In selecting the next process to work with, the operating system chooses a process with a low C value over one with a higher value. The entry in the <TT>STIME</TT> column is the time at which the process started.</P>
<P>To monitor every process on the system and get a full listing, enter <TT><B>ps -uax</B></TT>. By piping the command through the <TT>grep $LOGNAME</TT> command, the processes belonging to your login name are displayed while all others are filtered out. To see a full listing of all your processes, enter this:</P>
<!-- CODE SNIP //-->
<PRE>
<B>ps -uax | grep $LOGNAME</B>
</PRE>
<!-- END CODE SNIP //-->
<P>To list processes for two terminals (for example, tty1 and tty2), use the following command:
</P>
<!-- CODE SNIP //-->
<PRE>
$ <B>ps -t “1 2”</B>
PID TTY TIME COMMAND
32 tty01 0:05 bash
36 tty02 0:09 bash
235 tty02 0:16 vi calendar
</PRE>
<!-- END CODE SNIP //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="392-394.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="397-400.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 + -