📄 141-143.html
字号:
<HTML>
<HEAD>
<TITLE>Linux Unleashed, Third Edition:Basic Linux Commands</TITLE>
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!--ISBN=0672313723//-->
<!--TITLE=Linux Unleashed, Third Edition//-->
<!--AUTHOR=Tim Parker//-->
<!--PUBLISHER=Macmillan Computer Publishing//-->
<!--IMPRINT=Sams//-->
<!--CHAPTER=7//-->
<!--PAGES=141-143//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="138-141.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="143-145.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>Finally, the <TT><B>NAME</B></TT> column contains the name of the program you’re running. This is usually the command you type at the command line. However, sometimes the command you enter starts one or more processes, called <I>children</I>, and in this case, you’ll see these additional processes show up as well, without ever having typed them yourself. Your <TT><B>login</B></TT> shell will have a <TT><B>-</B></TT> before it, as in <TT><B>-bash</B></TT> in the previous example. This helps to distinguish this <I>primary</I> shell from any other shells you may enter from it. These will not have the <TT><B>-</B></TT> in front.</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>Note: </B><BR>If you are logged in as <TT><B>root</B></TT>, you see a list of <I>all</I> processes on the system. This is because the <TT><B>root</B></TT> username, being the superuser, owns everything that happens on the Linux system.
<P>If you are an “ordinary” user, but have also logged in on another terminal (including another virtual terminal you have selected by pressing Alt+F<I>n</I> as discussed in Chapter 6, “Getting Started”), you see the processes you are running on the other terminal (or terminals), as well.</P>
<P>Don’t be worried if you see slightly different columns of information when you use <TT><B>ps</B></TT>. The output tends to vary a little bit, especially with options in use, with different versions of Linux. The basic information is much the same, though.<HR></FONT>
</BLOCKQUOTE>
</P>
<P>One useful option with <TT><B>ps</B></TT> is <TT><B>u</B></TT>. Although it stands for “user,” it actually adds quite a few more columns of information in addition to just the username:</P>
<!-- CODE SNIP //-->
<PRE>
darkstar:~$ <TT><B>ps -u</B></TT>
USER PID %CPU %MEM SIZE RSS TTY STAT START TIME COMMAND
fido 41 0.1 6.8 364 472 v01 S 23:19 0:01 -bash
fido 138 0.0 3.3 72 228 v01 R 23:34 0:00 ps -u
</PRE>
<!-- END CODE SNIP //-->
<P>In addition to the username in the <TT><B>USER</B></TT> column, other interesting new items include <TT><B>%CPU</B></TT> which displays the percentage of your computer’s processing power that is being used by the process, and <TT><B>%MEM</B></TT> which displays the percentage of your computer’s memory that is being used by the process.</P>
<P>If you want to see all processes running on the system and not just the processes started by your own username, you can use the <TT><B>a</B></TT> command option. (The <TT><B>root</B></TT> login sees everyone’s processes automatically and does not have to use <TT><B>a</B></TT>, so <TT><B>root</B></TT> can get the following output by simply typing <TT><B>ps</B></TT>.)</P>
<!-- CODE //-->
<PRE>
darkstar:~$ <TT><B>ps -a</B></TT>
PID TTY STAT TIME COMMAND
62 v03 S 0:00 /sbin/agetty 38400 tty3
63 v04 S 0:00 /sbin/agetty 38400 tty4
64 v05 S 0:00 /sbin/agetty 38400 tty5
65 v06 S 0:00 /sbin/agetty 38400 tty6
330 v02 S 0:00 -bash
217 v01 S 0:00 -bash
217 v01 S 0:00 ps -a
</PRE>
<!-- END CODE //-->
<P>As you can see, quite a few “other” processes are happening on the system! In fact, most of the processes we see here are running whether or not anyone is actually logged into the Linux system. All the processes listed as running on <TT><B>tty psf</B></TT> are actually system processes and are started every time you boot up the Linux system. Processes of the form <TT><B>/sbin/agetty 38400 tty6</B></TT> are login processes running on a particular terminal waiting for your login.</P>
<P>It can be useful to combine the <TT><B>a</B></TT> and <TT><B>u</B></TT> options (if you’re not <TT><B>root</B></TT>).</P>
<!-- CODE //-->
<PRE>
darkstar:~$ <TT><B>ps -au</B></TT>
USER PID %CPU %MEM SIZE RSS TTY STAT START TIME COMMAND
root 72 0.0 3.6 390 532 v01 S 17:55 0:01 -bash
root 74 0.0 1.5 41 224 v03 S 17:55 0:00 /sbin/agetty
38400 tty3
root 75 0.0 1.5 41 224 v04 S 17:55 0:00 /sbin/agetty
38400 tty4
root 76 0.0 1.5 41 224 v05 S 17:55 0:00 /sbin/agetty
38400 tty5
root 77 0.0 1.5 41 224 v06 S 17:55 0:00 /sbin/agetty
38400 tty6
root 78 0.0 1.5 56 228 s00 S 17:55 0:00 gpm -t mman
root 98 0.0 1.5 41 224 v02 S 18:02 0:00 /sbin/agetty
38400 tty2
root 108 18.8 3.6 384 528 pp0 S 18:27 0:01 -bash
</PRE>
<!-- END CODE //-->
<P>A more technical <TT><B>l</B></TT> option can sometimes be useful:</P>
<!-- CODE SNIP //-->
<PRE>
darkstar:~$ <TT><B>ps -l</B></TT>
F UID PID PPID PRI NI SIZE RSS WCHAN STAT TTY TIME COMMAND
0 501 41 1 15 0 364 472 114d9c S v01 0:00 -bash
0 501 121 41 29 0 64 208 0 R v01 0:00 ps -l
</PRE>
<!-- END CODE SNIP //-->
<P>The interesting information is in the <TT><B>PPID</B></TT> column. PPID stands for “Parent Process ID”—in other words, the process that started the particular process. Notice that the <TT><B>ps -l</B></TT> command was started by <TT><B>-bash</B></TT>, the <TT><B>login</B></TT> shell. In other words, <TT><B>ps -l</B></TT> was started from the command line. Notice also that the PPID for the login shell is PID <TT><B>1</B></TT>. If you check the output from <TT><B>ps -au</B></TT> previously, you can see that the process with PID of <TT><B>1</B></TT> is <TT><B>init</B></TT>. The <TT><B>init</B></TT> process is the one that spawns, or starts, all other processes. If <TT><B>init</B></TT> dies, the system crashes!</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>Note: </B><BR>The Linux <TT><B>ps</B></TT> command has some quirks when it comes to options. First of all, the dash before the options is not necessary. In the earlier example, <TT><B>ps l</B></TT> would work the same as <TT><B>ps -l</B></TT>. Because most Linux commands <I>do</I> require the use of dashes with their command options and other versions of UNIX <I>might</I> require dashes when using <TT><B>ps</B></TT>, it’s best to use the dash anyway.
<P>Second, the order in which you enter the options does matter, especially if you try to combine the <TT><B>l</B></TT> and <TT><B>u</B></TT> options! Try typing <TT><B>ps -lu</B></TT>, and then <TT><B>ps -ul</B></TT>. This behavior is not covered in the <TT><B>ps</B></TT> man page. The moral is twofold: First, use the minimum possible number of command options. Second, the man pages are, alas, not always correct and complete.<HR></FONT>
</BLOCKQUOTE>
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="138-141.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="143-145.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 + -