📄 ch07.htm
字号:
<DL>
<DT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading32<FONT COLOR="#000077"><B>NOTE:</B> </FONT>If you are logged
in as <TT>root</TT>, you will see a list of all processes on the system. This is
because the <TT>root</TT> username, being the superuser, owns everything that happens
on the Linux system. If you are an "ordinary" user, but have also logged
in on another terminal (including another virtual terminal you have selected by pressing
Alt-Fn as discussed in Chapter 6, "Getting Started"), you will see the
processes you are running on the other terminal (or terminals) as well.
<HR>
</DL>
<P>One useful option with <TT>ps</TT> is <TT>u</TT>. Although it stands for "user,"
as in "List the username as well," it actually adds quite a few more columns
of information in addition to just the username:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">darkstar:~$ ps -u
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
</FONT></PRE>
<P>In addition to the username in the <TT>USER</TT> column, other interesting new
items include <TT>%CPU</TT>, which shows you what percentage of your computer's processing
power is being used by the process, and <TT>%MEM</TT>, which shows you what percentage
of your computer's memory 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>a</TT> command option. (The <TT>root</TT>
login sees everyone's processes automatically and does not have to use <TT>a</TT>,
so <TT>root</TT> can get the following output by simply typing <TT>ps</TT>.)<FONT
COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">darkstar:~$ ps -a
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
</FONT></PRE>
<P>As you can see, quite a few "other" processes are happening on the system!
In fact, most of the processes we see here will be running whether or not anyone
is actually logged into the Linux system. All the processes listed as running on
<TT>tty psf</TT> are actually system processes, and are started every time you boot
up the Linux system. Processes of the form <TT>/sbin/agetty 38400 tty6</TT> are login
processes running on a particular terminal waiting for your login.</P>
<P>It can be useful to combine the <TT>a</TT> and <TT>u</TT> options (if you're not
<TT>root</TT>).<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">darkstar:~$ ps -au
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
</FONT></PRE>
<P>A more technical <TT>l</TT> option can sometimes be useful:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">darkstar:~$ 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 interesting information is in the <TT>PPID</TT> column. PPID stands for "Parent
Process ID"--in other words, the process that started the particular process.
Notice that the <TT>ps -l</TT> command was started by <TT>-bash</TT>, the <TT>login</TT>
shell. In other words, <TT>ps -l</TT> was started from the command line. Notice also
that the PPID for the login shell is PID <TT>1</TT>. If you check the output from
<TT>ps -au</TT> above, you will see that the process with PID of <TT>1</TT> is <TT>init</TT>.
The <TT>init</TT> process is the one that spawns, or starts, all other processes.
If <TT>init</TT> dies, the system crashes!
<DL>
<DT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading33<FONT COLOR="#000077"><B>NOTE: </B></FONT>The Linux <TT>ps</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>ps l</TT> would work the same
as <TT>ps -l</TT>. Because most Linux commands do require the use of dashes with
their command options, and other versions of UNIX might require dashes when using
<TT>ps</TT>, it's best to use the dash anyway. Second, the order in which you enter
the options does matter, especially if you try to combine the <TT>l</TT> and <TT>u</TT>
options! Try typing <TT>ps -lu</TT>, and then <TT>ps -ul</TT>. This behavior is not
covered in the <TT>ps</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>
</DL>
<H4 ALIGN="CENTER"><A NAME="Heading34<FONT COLOR="#000077">The Process Termination
Command: kill</FONT></H4>
<P>The <TT>kill</TT> command is used to terminate processes that can't be stopped
by other means.
<DL>
<DT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading35<FONT COLOR="#000077"><B>NOTE:</B> </FONT>Before going through
the following procedure, if it's a program you're stuck in, make sure you can't stop
or exit it by typing Ctrl-C or some other key combination.
<HR>
</DL>
<DL>
<DD><B>1. </B>Switch to another virtual console and log in as <TT>root</TT>.<BR>
<B><BR>
2. </B>Run <TT>ps -u</TT> and identify the offending process. You will use its PID
in the next step.<BR>
<B><BR>
3.</B> Use the <TT>kill</TT> program by typing <TT>kill <</TT>PID<TT>></TT>,
where PID is the Process ID you want to kill. Make sure that you have correctly identified
the offending process! As root, you can kill any user process, including the wrong
one if you misread or mistype the PID.<BR>
<B><BR>
4.</B> Verify that the process has been killed by using <TT>ps -u</TT> again. You
can type <TT>ps -u <</TT>PID<TT>></TT>, which shows you the status of only
the specified PID. If there's a null result and you're just given the Linux prompt
again, the PID is dead, so go to step 8. However, it's best to look at the complete
<TT>ps -u</TT> list if it's not too long. Sometimes the offending process reappears
with a new PID! If that is the case, go to step 6.<BR>
<B><BR>
5. </B>If the process is still alive and has the same PID, use <TT>kill</TT>'s <TT>9</TT>
option. Type <TT>kill -9 <</TT>PID<TT>></TT>. Check it as in step 4. If this
does not kill the process, go to step 7. If the process is <BR>
now dead, go to step 8.<BR>
<B><BR>
6.</B> If the offending process has reappeared with a new PID, that means that it's
being created automatically by some other process. The only thing to do now is to
kill the parent process, which is the true offender! You might also have to kill
the parent process when <TT>kill -9</TT> does not work.<BR>
<B><BR>
7.</B> Use <TT>ps -l</TT> to identify the troublesome process's PPID. This is the
PID of the parent process. You should check the parent's identity more closely by
typing <TT>ps -u <</TT>Parent PID<TT>></TT> before going ahead and killing
it as described in step 3, using the PID of the parent in the <TT>kill</TT> command.
You should follow through with step 4 and, if necessary, step 5, making sure the
parent process has been killed.<BR>
<B><BR>
8.</B> The process is killed. Remember to log off. You should not leave <TT>root</TT>
logged in on virtual consoles, because you will forget that the <TT>root</TT> logins
are there!
<DT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading36<FONT COLOR="#000077"><B>NOTE:</B> </FONT>Sometimes processes
are simply unkillable! In this case, you're best off shutting down the Linux system
and rebooting.
<HR>
</DL>
<P>Linux keeps ordinary users (as opposed to root) from killing other users' processes
(maliciously or otherwise). For instance, if you are an ordinary user and you try
to kill the <TT>init</TT> process, which always has PID=1, you will see<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">darkstar:~$ kill 1
kill: (1) - Not owner
</FONT></PRE>
<P>Actually, not even <TT>root</TT> can kill the <TT>init</TT> process, although
there is no error message. The <TT>init</TT> process is one of those "unkillable"
processes discussed earlier, because it's such a key process. That's all for the
best!
<H3 ALIGN="CENTER"><A NAME="Heading37<FONT COLOR="#000077">Becoming Someone
Else: The su Command</FONT></H3>
<P>Usually, when you want to temporarily become a different user, you will simply
switch to an- other virtual terminal, log in as the other user, log out when you're
done, and return to your "home" virtual terminal. However, there are times
when this is impractical or inconvenient. Perhaps all your virtual terminals are
already busy, or perhaps you're in a situation (such as logged on via a telephone
and modem) in which you don't have virtual terminals available.</P>
<P>In these cases, you can use the <TT>su</TT> command. "su" stands for
"super user." If you type <TT>su</TT> by itself, you will be prompted for
the <TT>root</TT> password. If you successfully enter the <TT>root</TT> password,
you will see the <TT>root</TT> <TT>#</TT> prompt, and you will have all of <TT>root</TT>'s
privileges.</P>
<P>You can also become any other user by typing <TT>su </TT><username>. If
you are <TT>root</TT> when you type <TT>su </TT><username>, you are not asked
for that user's password since in principle you could change the user's password
or examine all the user's files from the <TT>root</TT> login anyway. If you are an
"ordinary" user trying to change to another ordinary user, you will be
asked to enter the password of the user you are trying to become.
<DL>
<DT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading38<FONT COLOR="#000077"><B>NOTE:</B> </FONT>Although <TT>su</TT>
grants you all the privileges you would get if you logged on as that user, be aware
that you won't inherit that login's exact environment or run that login's startup
files (if any). This means that <TT>su</TT> is not really suited to doing extended
work, and it's quite unsuitable for troubleshooting problems with that login.
<HR>
</DL>
<H3 ALIGN="CENTER"><A NAME="Heading39<FONT COLOR="#000077">The grep Command</FONT></H3>
<P>"What on earth does <TT>grep</TT> mean?" you ask.</P>
<P>This is a fair question. <TT>grep</TT> must be the quintessential UNIX acronym,
because it's impossible to understand even when it's spelled out in full!</P>
<P><TT>grep</TT> stands for Global Regular Expression Parser. You will understand
the use of this command right away, but when "Global Regular Expression Parser"
becomes a comfortable phrase in itself, you should probably consider taking a vacation.</P>
<P>What <TT>grep</TT> does, essentially, is find and display lines that contain a
pattern that you specify. There are two basic ways to use <TT>grep</TT>.</P>
<P>The first use of <TT>grep</TT> is to filter the output of other commands. The
general syntax is <TT><</TT>command<TT>> | grep <</TT>pattern<TT>></TT>.
For instance, if we wanted to see every actively running process on the system, we
would type <TT>ps -a | grep R</TT>. In this application, <TT>grep</TT> passes on
only those lines that contain the pattern (in this case, the single letter) R. Note
that if someone were running a program called <TT>Resting</TT>, it would show up
even if its status were <TT>S</TT> for sleeping, because <TT>grep</TT> would match
the R in <TT>Resting</TT>. An easy way around this problem is to type <TT>grep "
R "</TT>, which explicitly tells <TT>grep</TT> to search for an R with a space
on each side. You must use quotes whenever you search for a pattern that contains
one or more blank spaces.</P>
<P>The second use of <TT>grep</TT> is to search for lines that contain a specified
pattern in a specified file. The syntax here is <TT>grep <</TT>pattern<TT>>
<</TT>filename<TT>></TT>. Be careful. It's easy to specify the filename first
and the pattern second by mistake! Again, you should be as specific as you can with
the pattern to be matched, in order to avoid "false" matches.
<H3 ALIGN="CENTER"><A NAME="Heading40<FONT COLOR="#000077">Summary</FONT></H3>
<P>By this point you should have tried enough different Linux commands to start getting
familiar (if not yet entirely comfortable) with typical Linux usage conventions.</P>
<P>It is important that you be able to use the man pages provided online by Linux.
A very good exercise at this point is to pull up man pages for all the commands we
have looked at in the past two chapters: <TT>login</TT>, <TT>passwd</TT>, <TT>who</TT>,
<TT>adduser</TT>, and so on. If some of the commands listed under "See also:"
look interesting, by all means take a look at their man pages too!
<DL>
<DT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading41<FONT COLOR="#000077"><B>NOTE:</B> </FONT>Some man pages,
such as the one for <TT>bash</TT>, are extremely long. Do not plan to read them all
in one sitting!
<HR>
</DL>
<P>In Chapter 8, we head out from "home" and poke around in the Linux file
system. As system administrators, we should know what our hard drives contain! For
instance, there are special "administrator-only" directories crammed with
goodies.</P>
<P>Several more "essential" commands will be introduced. By the end of
the next chapter, you will have seen and tried most of the important "user"
Linux commands and will have had a taste of some of the "administrator"
commands.
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -