📄 ch40.htm
字号:
<PRE><FONT COLOR="#0066FF">$ 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
</FONT></PRE>
<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 look-up 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 proce ss 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 root, 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 root and want to see only Yvonne's processes, you could issue this
command:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">ps -u yvonne
</FONT></PRE>
<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 on 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 root 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 root), the <TT>-a</TT> option produces the following output:<FONT
COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">$ 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
</FONT></PRE>
<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):<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">$ 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
</FONT></PRE>
<P>The <TT>-au</TT> options produce a list with all the same columns as the <TT>-u</TT>
option, but show 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):<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">$ 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
</FONT></PRE>
<P>The <TT>PPID</TT> (Parent Process ID) column shows which process started that
particular process. You will see in the extract from the preceding output that the
<TT>ps</TT> command itself was started by a <TT>bash</TT> process, because the shell
is the entity that is the parent of all user commands. You also see that the PPID
for the login Bourne shell is PID "1", which is the <TT>init</TT> process
of the operating system. (If you think about what this means, it implies that if
<TT>init</TT> ever terminates, all other processes die, too. Simply put, when <TT>init</TT>
dies, the entire system is off.)
<DL>
<DT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading10<FONT COLOR="#000077"><B>NOTE:</B> </FONT>The Linux version
of the <TT>ps</TT> command has a few idiosyncrasies. The hyphen before any options
is not essential, so <TT>ps u</TT> would work in the same manner as <TT>ps -u</TT>.
However, because UNIX convention (and most UNIX versions) requires hyphens, you should
use them.
<HR>
</DL>
<P>For System Administrators Most system administrators get by with three versions
of the <TT>ps</TT> command (when logged in as root). To display information about
the system as a whole, the following command lines show practically everything there
is to know about processes:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">ps -ax
ps -aux
ps -le
</FONT></PRE>
<P>The meaning of the primary columns in the output from the two commands has been
mentioned earlier in this section. The rest of the columns are either evident from
their shortform or not that important. For complete information, see the <TT>ps</TT>
man page (which is not entirely accurate or complete, unfortunately).
<H3 ALIGN="CENTER"><A NAME="Heading11<FONT COLOR="#000077">Using kill</FONT></H3>
<P>Occasionally you will find a process that has locked up a terminal or isn't doing
anything, which is generally referred to as a "hung" process. Sometimes
a user will have a process that doesn't terminate properly (especially common with
programmers). These are "runaway" processes. In both cases, the only way
to get rid of the process and restore some normality to the system is to terminate
the process entirely. This is done with the <TT>kill</TT> command.</P>
<P>To use <TT>kill</TT>, you have to have access to another window or console where
you can issue commands. If your terminal is completely locked up, you have to find
another one to log in on. As a user, you can only kill your own processes--you cannot
affect any process another user on the system is running. As root, you can terminate
any process with the <TT>kill</TT> command.</P>
<P>In order to use the <TT>kill</TT> command, you need the process ID number (PID)
of the process to be terminated. You have to obtain the PID with the <TT>ps</TT>
command and note the PID. Next, use the <TT>kill</TT> command with the PID as an
argument. For example, the following terminal session shows a user process started
by Walter called <TT>bad_prog</TT>, that has hung up and needs to be killed. The
PID is obtained by displaying all of the system's processes with their usernames
(we've cut the other lines from the <TT>ps</TT> command output for simplicity's sake):<FONT
COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">$ ps -u
USER PID %CPU %MEM SIZE RSS TTY STAT START TIME COMMAND
walter 561 0.1 6.8 364 472 v01 S 13:19 0:01 -bash
walter 598 9.3 4.1 2736 472 v01 R 15:26 2:01 bad_prog
$ kill 598
</FONT></PRE>
<P>When you issue the <TT>kill</TT> command, you don't get any return message if
it works properly. The only way to verify that the process termination has been properly
conducted is to issue another <TT>ps</TT> command and look for the PID or process
name.
<H4 ALIGN="CENTER"><A NAME="Heading12<FONT COLOR="#000077">killing Child Processes</FONT></H4>
<P>Because some processes spawn child processes with different PIDs, you must be
sure to check that all the child processes are terminated. The best way to do this
is to watch the names of the executing processes for a few minutes to ensure the
child isn't dormant, only to return later. This problem usually happens when the
child processes are being generated by a parent. You should check the <TT>PPID</TT>
column (use the <TT>ps -l</TT> option) to see which process is the parent and terminate
that.
<DL>
<DT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading13<FONT COLOR="#000077"><B>WARNING: </B></FONT>When you are
killing processes and logged in as root, make sure you type the correct PID, or you
may inadvertently terminate another process. Check the PID carefully! Also, don't
kill any system processes unless you know what they do and why they need to be terminated.
<HR>
</DL>
<P>If the process doesn't terminate properly with the <TT>kill</TT> command, you
need to use sterner measures. The <TT>kill</TT> command actually has several levels
of operation. When issued wi
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -