📄 612-615.html
字号:
<HTML>
<HEAD>
<TITLE>Linux Unleashed, Third Edition:Processes</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=34//-->
<!--PAGES=612-615//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="609-612.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="615-618.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading7"></A><FONT COLOR="#000077">For the Superuser</FONT></H4>
<P>When normal users issue the <TT>ps</TT> command, they see only their own processes. If you issue the <TT>ps</TT> command when you are logged in as the superuser (usually <TT>root</TT>, although you can change the name), you will see all the processes on the system because the <TT>root</TT> login owns everything running. This can produce very long outputs, especially on a system with several users, so you will probably want to pipe the output from the <TT>ps</TT> command to a page filter (such as <TT>more</TT> or <TT>less</TT>) or save the output in a file for further examination. Both commands are shown here:</P>
<!-- CODE SNIP //-->
<PRE>
ps | more
ps > /tmp/ps_file
</PRE>
<!-- END CODE SNIP //-->
<H4 ALIGN="LEFT"><A NAME="Heading8"></A><FONT COLOR="#000077">Useful ps options</FONT></H4>
<P>A useful <TT>ps</TT> option for checking user processes is <TT>-u</TT>, which adds several columns to the output of the <TT>ps</TT> command. The output from a user (not <TT>root</TT>) command using this option looks like this:</P>
<!-- CODE SNIP //-->
<PRE>
$ ps -u
USER PID %CPU %MEM SIZE RSS TTY STAT START TIME COMMAND
bill 41 0.1 6.8 364 472 v01 S 23:19 0:01 -bash
bill 138 0.0 3.3 72 228 v01 R 23:34 0:00 ps -u
</PRE>
<!-- END CODE SNIP //-->
<P>The most important addition to the output is the <TT>USER</TT> column, which shows who started and owns the process. The name listed under the <TT>USER</TT> column is the user’s login name, as found in the <TT>/etc/passwd</TT> file. (<TT>ps</TT> does a lookup in the <TT>/etc/passwd</TT> file to convert the user ID number—UID—to the proper username.)</P>
<P>This option also adds the column labeled <TT>%CPU</TT> which shows the percentage of CPU time that has been used by the process so far. The column <TT>%MEM</TT> shows the percentage of your system’s memory currently used by the process. These numbers can be handy for finding processes that consume far too much CPU or memory, called “CPU hogs” and “memory hogs” by most administrators. If you see a user process that has very high usage, it is worth checking to make sure it is a valid process and not a runaway that will continue to grind at your system’s resources.</P>
<P>When you issue this command logged in as <TT>root</TT>, you see all the processes running on the system. As before, you should consider paginating the output to make it readable. With some versions of Linux’s <TT>ps</TT> command, you can also use the <TT>-u</TT> option to specify a user’s processes by adding each username. For example, if you are logged in as <TT>root</TT> and want to see only Yvonne’s processes, you could issue the following command:</P>
<!-- CODE SNIP //-->
<PRE>
ps -u yvonne
</PRE>
<!-- END CODE SNIP //-->
<P>This format of the <TT>-u</TT> option works with System V versions of <TT>ps</TT>, but not the BSD-based version of <TT>ps</TT> included with most Linux distributions (including the one of the CD-ROM). You can obtain other versions of <TT>ps</TT> on FTP and BBS sites. Most users can issue this command to examine other users’ processes, as well. This lets them find out who is hogging all the CPU time! It also lets the superuser see the processes that users are running when they report problems, without having to wade through all the system processes, as well.</P>
<P>Users can also see all the processes running on the system (instead of just the processes started by them) by using the <TT>-a</TT> option. Because the superuser sees all the processes on the system anyway, the <TT>root</TT> login doesn’t have to use this option, although it is still legal to use it. This output doesn’t change, though. When issued by a user (not <TT>root</TT>), the <TT>-a</TT> option produces the following output:</P>
<!-- CODE //-->
<PRE>
$ ps -a
PID TTY STAT TIME COMMAND
1 psf S 0:00 init
6 psf S 0:00 update (sync)
23 psf S 0:00 /usr/sbin/crond -l10
29 psf S 0:00 /usr/sbin/syslogd
31 psf S 0:00 /usr/sbin/klogd
33 psf S 0:00 /usr/sbin/lpd
40 psf S 0:00 selection -t ms
42 v02 S 0:01 -bash
43 v03 S 0:00 /sbin/agetty 38400 tty3
44 v04 S 0:00 /sbin/agetty 38400 tty4
45 v05 S 0:00 /sbin/agetty 38400 tty5
46 v06 S 0:00 /sbin/agetty 38400 tty6
41 v01 S 0:01 -bash
140 v01 R 0:00 ps -a
</PRE>
<!-- END CODE //-->
<P>This is a relatively short output showing a very lightly loaded system. Most of the entries are the Linux operating system kernel and daemons, as well as serial port <TT>getty</TT> processes. Only the last two commands were started by the user who issued the <TT>ps</TT> command. Of course, you can’t tell who started each process with this output, so you can combine the <TT>-u</TT> and <TT>-a</TT> options (note that you use only one hyphen, followed by the option letters):</P>
<!-- CODE //-->
<PRE>
$ ps -au
USER PID %CPU %MEM SIZE RSS TTY STAT START TIME COMMAND
root 64 0.0 1.5 41 224 v02 S 22:25 0:00 /sbin/agetty
38400 tty2
root 65 0.0 1.5 41 224 v03 S 22:25 0:00 /sbin/agetty
38400 tty3
root 66 0.0 1.5 41 224 v04 S 22:25 0:00 /sbin/agetty
38400 tty4
root 67 0.0 1.5 41 224 v05 S 22:25 0:00 /sbin/agetty
38400 tty5
root 68 0.0 1.5 41 224 v06 S 22:25 0:00 /sbin/agetty
38400 tty6
root 69 0.0 1.5 56 228 s00 S 22:25 0:00 gpm -t mman
root 71 0.3 3.6 388 532 pp0 S 22:26 0:02 -bash
root 155 0.0 1.5 77 220 pp0 R 22:37 0:00 ps -au
tparker 119 0.4 3.5 372 520 v01 S 22:32 0:01 -bash
tparker 132 0.1 2.2 189 324 v01 S 22:33 0:00 vi test
</PRE>
<!-- END CODE //-->
<P>The <TT>-au</TT> options produce a list with all the same columns as the <TT>-u</TT> option but shows all the processes running on the system. The order in which you enter the options doesn’t matter, so <TT>-au</TT> is functionally the same as <TT>-ua</TT>. When you are adding several options, this can be handy.</P>
<P>A few other <TT>ps</TT> command line options are occasionally useful. The <TT>-l</TT> option adds information about which processes started each process (useful when you want to identify child processes):</P>
<!-- CODE SNIP //-->
<PRE>
$ ps -l
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><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="609-612.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="615-618.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 + -