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

📄 rhl07.htm

📁 linux的初学电子书
💻 HTM
📖 第 1 页 / 共 4 页
字号:
<BR>

<P>The STAT column gives the status of the process. The two most common entries in the status column are S for sleeping and R for running. A sleeping process is one that isn't currently active. However, don't be misled. A sleeping process might just be 
taking a very brief catnap! In fact, a process might switch between sleeping and running many times every second.

<BR>

<P>The TIME column shows the amount of system time used by the process. Clearly, neither of our processes are taking up any appreciable system time!

<BR>

<P>Finally, the COMMAND column contains the name of the program you're running. This will usually be the command you typed at the command line. However, sometimes the command you type starts one or more child processes, and in this case, you would see 
these additional processes show up as well, without ever having typed them yourself. Your login shell will have a - before it, as in -bash in the previous example. This helps to distinguish this primary shell from any shells you might enter from it. These 
will not have the - in front.

<BR>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE>If you are logged in as root, you will see a list of all processes on the system. This is because the root username, being the superuser, owns everything that happens on the Linux system.

<BR>If you are an &quot;ordinary&quot; user, but have also logged in on another terminal (including another virtual terminal you have selected by pressing Alt-Fn as discussed in <A HREF="rhl06.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl06.htm">Chapter 6</A>, &quot;Getting Started with Linux&quot;), 
you will see the processes you are running on the other terminal (or terminals) as well.</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<P>One useful option with ps is u. Although it stands for &quot;user,&quot; as in &quot;List the username as well,&quot; it actually adds quite a few more columns of information in addition to just the username:

<BR>

<PRE>

<FONT COLOR="#000080">darkstar:~$ ps -u

USER PID %CPU %MEM SIZE RSS TTY STAT START TIME COMMAND

fido 41 0.1 6.8 364 472 v01 S&lt; 23:19 0:01 -bash

fido 138 0.0 3.3 72 228 v01 R&lt; 23:34 0:00 ps -u</FONT></PRE>

<P>In addition to the username in the USER column, other interesting new items include %CPU, which shows you what percentage of your computer's processing power is being used by the process, and %MEM, which shows you what percentage of your computer's 
memory is being used by the process.

<BR>

<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 a command option. (The root login sees everyone's processes automatically and does not have to use a, so root can get the 
following output by simply typing ps.)

<BR>

<PRE>

<FONT COLOR="#000080">darkstar:~$ ps -a

PID TTY STAT TIME COMMAND

62 v03 S&lt; 0:00 /sbin/agetty 38400 tty3

63 v04 S&lt; 0:00 /sbin/agetty 38400 tty4

64 v05 S&lt; 0:00 /sbin/agetty 38400 tty5

65 v06 S&lt; 0:00 /sbin/agetty 38400 tty6

330 v02 S&lt; 0:00 -bash

217 v01 S&lt; 0:00 -bash

217 v01 S&lt; 0:00 ps -a</FONT></PRE>

<P>As you can see, quite a few &quot;other&quot; 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 tty 
psf are actually system processes, and are started every time you boot up the Linux system. Processes of the form /sbin/agetty 38400 tty6 are login processes running on a particular terminal waiting for your login.

<BR>

<P>It can be useful to combine the a and u options (if you're not root).

<BR>

<PRE>

<FONT COLOR="#000080">darkstar:~$ ps -au

USER PID %CPU %MEM SIZE RSS TTY STAT START TIME COMMAND

root 72 0.0 3.6 390 532 v01 S&lt; 17:55 0:01 -bash

root 74 0.0 1.5 41 224 v03 S&lt; 17:55 0:00 /sbin/agetty 38400 tty3

root 75 0.0 1.5 41 224 v04 S&lt; 17:55 0:00 /sbin/agetty 38400 tty4

root 76 0.0 1.5 41 224 v05 S&lt; 17:55 0:00 /sbin/agetty 38400 tty5

root 77 0.0 1.5 41 224 v06 S&lt; 17:55 0:00 /sbin/agetty 38400 tty6

root 78 0.0 1.5 56 228 s00 S&lt; 17:55 0:00 gpm -t mman

root 98 0.0 1.5 41 224 v02 S&lt; 18:02 0:00 /sbin/agetty 38400 tty2

root 108 18.8 3.6 384 528 pp0 S&lt; 18:27 0:01 -bash</FONT></PRE>

<P>A more technical l option can sometimes be useful:

<BR>

<PRE>

<FONT COLOR="#000080">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&lt; v01 0:00 -bash

0 501 121 41 29 0 64 208 0 R&lt; v01 0:00 ps -l</FONT></PRE>

<P>The interesting information is in the PPID column. PPID stands for &quot;Parent Process ID&quot;&#151;in other words, the process that started the particular process. Notice that the ps -l command was started by -bash, the login shell. In other words, 
ps -l was started from the command line. Notice also that the PPID for the login shell is PID 1. If you check the output from ps -au shown previously, you will see that the process with PID of 1 is init. The init process is the one that spawns, or starts, 
all other processes. If init dies, the system crashes!

<BR>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE>The Linux ps command has some quirks when it comes to options.

<BR>First of all, the dash before the options is not necessary. In the earlier example, ps l would work the same as ps -l. Because most Linux commands do require the use of dashes with their command options, and other versions of UNIX might require dashes 
when using ps, it's best to use the dash anyway.

<BR>Second, the order in which you enter the options does matter, especially if you try to combine the l and u options! Try typing ps -lu, and then ps -ul. This behavior is not covered in the ps 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.</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<BR>

<A NAME="E69E46"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>The Process Termination Command kill</B></FONT></CENTER></H4>

<BR>

<P>The kill command is used to terminate processes that can't be stopped by other means.

<BR>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

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

<BR>

<OL>

<LI><A NAME="I2"></A>Switch to another virtual console and log in as root.

<BR>

<BR>

<LI>Run ps -u and identify the offending process. You will use its PID in the next step.

<BR>

<BR>

<LI>Use the kill program by typing kill &lt;PID&gt;, where PID is the Process ID you wish 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>

<BR>

<LI>Verify that the process has been killed by using ps -u again. You can type ps -u &lt;PID&gt;, 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 ps -u 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>

<BR>

<LI>If the process is still alive and has the same PID, use kill's 9 option. Type kill -9 &lt;PID&gt;. Check it as in step 4. If this does not kill the process, go to step 7. If the process is now dead, go to step 8.

<BR>

<BR>

<LI>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 kill -9 does not work.

<BR>

<BR>

<LI>Use ps -l 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 ps -u &lt;Parent PID&gt; before going ahead and killing it as described in step 3, using the PID 
of the parent in the kill command. You should follow through with step 4 and, if necessary, step 5, making sure the parent process has been killed.

<BR>

<BR>

<LI>The process is killed. Remember to log off. You should not leave root logged in on virtual consoles, because you will forget that the root logins are there!

<BR>

<BR>

</OL>

<BR>

<NOTE>Sometimes processes are simply unkillable! In this case, you're best off shutting down the Linux system and rebooting.</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<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 init process, which always has PID=1, you will see

<BR>

<PRE>

<FONT COLOR="#000080">darkstar:~$ kill 1

kill: (1) - Not owner</FONT></PRE>

<P>Actually, not even root can kill the init process, although there is no error message. The init process is one of those &quot;unkillable&quot; processes discussed earlier, because it's such a key process. That's all for the best!

<BR>

<BR>

<A NAME="E68E49"></A>

<H3 ALIGN=CENTER>

<CENTER>

<FONT SIZE=5 COLOR="#FF0000"><B>Becoming Someone Else The su Command</B></FONT></CENTER></H3>

<BR>

<P>Usually, when you want to temporarily become a different user, you will simply switch to another virtual terminal, log in as the other user, log out when you're done, and return to your &quot;home&quot; 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.

<BR>

<P>In these cases, you can use the su command. &quot;su&quot; stands for &quot;super user.&quot; If you type su by itself, you will be prompted for the root password. If you successfully enter the root password, you will see the root # prompt, and you will 
have all of root's privileges.

<BR>

<P>You can also become any other user by typing su &lt;username&gt;. If you are root when you type su &lt;username&gt;, 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 root login anyway. If you are an &quot;ordinary&quot; user trying to change to another ordinary user, you will be asked to enter the password of the user you are trying to become.

<BR>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE>Although su 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 su is not really suited to doing 
extended work, and it's quite unsuitable for troubleshooting problems with that login.</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<BR>

<A NAME="E68E50"></A>

<H3 ALIGN=CENTER>

<CENTER>

<FONT SIZE=5 COLOR="#FF0000"><B>The grep Command</B></FONT></CENTER></H3>

<BR>

<P>&quot;What on earth does grep mean?&quot; you ask.

<BR>

<P>This is a fair question. grep must be the quintessential UNIX acronym, because it's impossible to understand even when it's spelled out in full!

<BR>

<P>grep stands for Global Regular Expression Parser. You will understand the use of this command right away, but when &quot;Global Regular Expression Parser&quot; becomes a comfortable phrase in itself, you should probably consider taking a vacation.

<BR>

<P>What grep does, essentially, is find and display lines that contain a pattern that you specify. There are two basic ways to use grep.

<BR>

<P>The first use of grep is to filter the output of other commands. The general syntax is &lt;command&gt; | grep &lt;pattern&gt;. For instance, if we wanted to see every actively running process on the system, we would type ps -a | grep R. In this 
application, grep 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 Resting, it would show up even if its status were S for sleeping, because grep would match the R 
in Resting. An easy way around this problem is to type grep &quot; R &quot;, which explicitly tells grep 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.

<BR>

<P>The second use of grep is to search for lines that contain a specified pattern in a specified file. The syntax here is grep &lt;pattern&gt; &lt;filename&gt;. 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 &quot;false&quot; matches.

<BR>

<BR>

<A NAME="E68E51"></A>

<H3 ALIGN=CENTER>

<CENTER>

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

<BR>

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

<BR>

<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: login, passwd, who, adduser, and so on. If some 
of the commands listed under &quot;See also:&quot; look interesting, by all means take a look at their man pages too!

<BR>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE>Some man pages, such as the one for bash, are extremely long. Do not plan to read them all in one sitting!</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<P>In <A HREF="rhl08.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl08.htm">Chapter 8</A>, we head out from &quot;home&quot; and poke around in the Linux filesystem. As system administrators, we should know what our hard drives contain! For instance, there are special &quot;administrator-only&quot; 
directories crammed with goodies.

<BR>

<P>Several more &quot;essential&quot; commands will be introduced. By the end of the next chapter, you will have seen and tried most of the important &quot;user&quot; Linux commands and will have had a taste of some of the &quot;administrator&quot; 
commands.

<P ALIGN=LEFT>

<A HREF="rhl06.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl06.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="rhl08.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl08.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 + -