⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rhl40.htm

📁 linux的初学电子书
💻 HTM
📖 第 1 页 / 共 2 页
字号:
examination. Both commands are shown here:

<BR>

<PRE>

<FONT COLOR="#000080">ps | more

ps &gt; /tmp/ps_file</FONT></PRE>

<BR>

<A NAME="E69E457"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>Useful </B><B>ps</B><B> options</B></FONT></CENTER></H4>

<BR>

<P>A useful ps option for checking user processes is -u, which adds several columns to the output of the ps command. The output from a user (not root) command using this option looks like this:

<BR>

<PRE>

<FONT COLOR="#000080">$ 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 USER column, which shows who started and owns the process. The name listed under the USER column is the user's login name, as found in the /etc/passwd file. (ps does a look-up in the /etc/passwd file to 
convert the user ID number&#151;UID&#151;to the proper user name.)

<BR>

<P>This option also adds the column labeled %CPU which shows the percentage of CPU time that has been used by the process so far. The column %MEM 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 &quot;CPU hogs&quot; and &quot;memory hogs&quot; 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 
run-away that will continue to grind at your system's resources.

<BR>

<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 ps command, you can also use the -u 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 the command:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">ps -u yvonne</FONT></PRE>

<P>This format of the -u option works with System V versions of ps, but not the BSD-based version of ps included with most Linux distributions (including the one of the CD-ROM). You can obtain other versions of ps 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.

<BR>

<P>Users can also see all the processes running on the system (instead of just the processes started by them) by using the -a 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 -a option produces the following output:

<BR>

<PRE>

<FONT COLOR="#000080">$ 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 getty processes. Only the last two commands were started by the user who issued the ps 
command. Of course, you can't tell who started each process with this output, so you can combine the -u and -a options (note that you use only one hyphen, followed by the option letters):

<BR>

<PRE>

<FONT COLOR="#000080">$ 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 -au options produce a list with all the same columns as the -u option, but shows all the processes running on the system. The order in which you enter the options doesn't matter, so -au is functionally the same as -ua. When you are adding several 
options, this can be handy.

<BR>

<P>A few other ps command-line options are occasionally useful. The -l option adds information about which processes started each process (useful when you want to identify child processes):

<BR>

<PRE>

<FONT COLOR="#000080">$ 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 PPID (Parent Process ID) column shows which process started that particular process. You will see in the extract from the preceding output, that the ps command itself was started by a bash 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 &quot;1&quot;, which is the init process of the operating system. (If you think about what this means, it implies that if init ever terminates, all other processes die, too. 
Simply put, when init dies, the entire system is off.)

<BR>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE>The Linux version of the ps command has a few idiosyncrasies. The hyphen before any options is not strictly necessary, so ps u would work in the same manner as ps -u. However, because UNIX convention (and most UNIX versions) require hyphens, you 
should use them.</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<BR>

<A NAME="E70E11"></A>

<H5 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>For System Administrators</B></FONT></CENTER></H5>

<BR>

<P>Most system administrators get by with three versions of the ps 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:

<BR>

<PRE>

<FONT COLOR="#000080">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 ps man page 
(which is not entirely accurate or complete, unfortunately).

<BR>

<BR>

<A NAME="E68E342"></A>

<H3 ALIGN=CENTER>

<CENTER>

<FONT SIZE=5 COLOR="#FF0000"><B>Using </B><B>kill</B></FONT></CENTER></H3>

<BR>

<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 &quot;hung&quot; process. Sometimes a user will have a process that doesn't terminate properly (especially common with 
programmers). These are &quot;run-away&quot; 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 kill command.

<BR>

<P>To use kill, 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&#151;you cannot affect 
any process another user or the system is running. As root, you can terminate any process with the kill command.

<BR>

<P>In order to use the kill command, you need the process ID number (PID) of the process to be terminated. You have to obtain the PID with the ps command and note the PID. Next, use the kill command with the PID as an argument. For example, the following 
terminal session shows a user process started by Walter called bad_prog, that has hung up and needs to be killed. The PID is obtained by displaying all of the system's processes with their user names (we've cut the other lines from the ps command output 
for simplicity's sake):

<BR>

<PRE>

<FONT COLOR="#000080">$ 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 kill 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 ps command and look for the PID or process name.

<BR>

<BR>

<A NAME="E69E458"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>kill</B><B>ing Child Processes</B></FONT></CENTER></H4>

<BR>

<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 PPID column (use the ps -l option) to see which process is the parent and terminate that.

<BR>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE>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.</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<P>If the process doesn't terminate properly with the kill command, you need to use sterner measures. The kill command actually has several levels of operation. When issued with no arguments other than the PID, kill tries to gracefully terminate the 
process (which means any open files are closed, and generally, kill is polite to the process). If this doesn't work, you should use the -9 option, which is a little more forceful in its attempt to terminate the process. For example, to forcefully terminate 
the process with PID 726, issue the following command:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">kill -9 726</FONT></PRE>

<P>If that doesn't work, then the process may be unkillable. This does happen occasionally with Linux, and the only solution is to shut down and reboot the machine.

<BR>

<BR>

<A NAME="E69E459"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>kill</B><B>ing Rights</B></FONT></CENTER></H4>

<BR>

<P>To help prevent a user from killing another user's processes, ps checks for the process owner. If a user tries to kill another user's process, a message like this one is displayed:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">kill: - Not owner</FONT></PRE>

<P>The superuser doesn't get this message because the superuser login can kill anything.

<BR>

<BR>

<A NAME="E68E343"></A>

<H3 ALIGN=CENTER>

<CENTER>

<FONT SIZE=5 COLOR="#FF0000"><B>Summary</B></FONT></CENTER></H3>

<BR>

<P>This chapter has shown you how to obtain listings of the processes currently executing on your Linux system and how to terminate them when they require it. Although you may not have to use this knowledge often, every operating system has occasions where 
something gets out of hand and you need to control it. The problems multiply as the number of users increases. Instead of rebooting the Linux system, process commands enable you to correct the problem without terminating the operating system.<A 
NAME="I2"></A>

<P ALIGN=LEFT>

<A HREF="rhl39.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl39.htm" TARGET="_self"><IMG SRC="purprev.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A>

<A HREF="#I0" TARGET="_self"><IMG SRC="purtop.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purtop.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Page Top"></A>

<A HREF="index-1.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/index.htm" TARGET="_self"><IMG SRC="purtoc.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purtoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A>

<A HREF="rhl41.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl41.htm" TARGET="_self"><IMG SRC="purnext.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purnext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A>


</BODY></HTML>



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -