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

📄 lsg20.htm

📁 linux-unix130.linux.and.unix.ebooks130 linux and unix ebookslinuxLearning Linux - Collection of 12 E
💻 HTM
📖 第 1 页 / 共 3 页
字号:


<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
        var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>

 -->


























<LINK REL="ToC" HREF="index.htm">







<LINK REL="Index" HREF="htindex.htm">







<LINK REL="Next" HREF="lsg21.htm">



















<A NAME="I0"></A>







<H2>Linux System Administrator's Survival Guide lsg20.htm</H2>







<P ALIGN=LEFT>







































<HR ALIGN=CENTER>







<P>







<UL>







<UL>







<UL>







<LI>







<A HREF="#E68E108" >Understanding Processes</A>







<LI>







<A HREF="#E68E109" >Using the ps Command</A>







<LI>







<A HREF="#E68E110" >Using kill</A>







<LI>







<A HREF="#E68E111" >Using the top Command</A>







<LI>







<A HREF="#E68E112" >Summary</A></UL></UL></UL>







<HR ALIGN=CENTER>







<A NAME="E66E23"></A>







<H1 ALIGN=CENTER>







<CENTER>







<FONT SIZE=6 COLOR="#FF0000"><B>Chapter 20</B></FONT></CENTER></H1>







<BR>







<A NAME="E67E26"></A>







<H2 ALIGN=CENTER>







<CENTER>







<FONT SIZE=6 COLOR="#FF0000"><B>Managing Processes</B></FONT></CENTER></H2>







<BR>







<P>Everything that runs on a Linux system is a process. Knowing how to manage the processes running on your Linux system is a critical aspect of system administration. This chapter tells you how to find out which processes are running on your system and what they are doing. You can then use this information to manage the processes as necessary.







<BR>







<P>In the course of discussing processes, this chapter doesn't bother explaining the mechanics behind how processes are allocated or how the Linux kernel manages to time slice all the processes to run a multitasking operating system. Instead, this chapter looks at the nitty-gritty aspects of process control you need to keep your system running smoothly.







<BR>







<BR>







<A NAME="E68E108"></A>







<H3 ALIGN=CENTER>







<CENTER>







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







<BR>







<P>You may hear the terms process and job used when talking about operating systems. A formal definition of a process is that it is a single program running in its own virtual address space. Using this definition, everything running under Linux is a process. A job, on the other hand, may involve several commands executing in series. Likewise, a single command line issued at a shell prompt may involve more than one process, especially when pipes or redirection are involved.







<BR>







<P>Several types of processes are involved with the Linux operating system. Each has its own special features and attributes:







<BR>







<UL>







<UL>







<P>An interactive process is a process initiated from (and controlled by) a shell. Interactive processes may be in foreground or background.







<BR>







</UL></UL>







<UL>







<UL>







<P>A batch process is a process that is not associated with a terminal but is submitted to a queue to be executed sequentially.







<BR>







</UL></UL>







<UL>







<UL>







<P>A daemon process is a process that runs in the background until it's required. This kind of processes is usually initiated when Linux boots.







<BR>







</UL></UL>







<BR>







<A NAME="E68E109"></A>







<H3 ALIGN=CENTER>







<CENTER>







<FONT SIZE=5 COLOR="#FF0000"><B>Using the ps Command</B></FONT></CENTER></H3>







<BR>







<P>The easiest method of finding out which processes are running on your system is to use the ps (process status) command. The ps command is available to all system users, as well as root, although the output changes a little depending on whether you are logged in as root when you issue the command. When you are logged in as a normal system user (not root) and issue the ps command by itself, it displays information about every process you are running. The following output is an example of what you might see:







<BR>







<PRE>







<FONT COLOR="#000080">$ ps







 PID TTY STAT TIME COMMAND







 41 v01 S 0:00 -bash







 134 v01 R 0:00 ps</FONT></PRE>







<P>The output of the ps command is always organized in columns. The first column is labeled PID, which means process identification number. The PID is a number that Linux assigns to each process to help in handling all processes. PIDs start at zero and increment by one for each process being run, up to some system-determined number (such as 65,564). When Linux reaches the highest number, it starts numbering from the lowest number again, skipping the numbers used by active processes. Usually, the lowest number processes are the system kernel and daemons, which start when Linux boots and remain active as long as Linux is running. To manipulate processes (to terminate them, for example), you must use the PID.







<BR>







<P>The TTY column in the ps command output shows you which terminal the process was started from. If you are logged in as a user, this column usually lists your terminal or console window. If you are running on multiple console windows, you see all the processes you started in every displayed window.







<BR>







<P>The STAT column in the ps command output shows you the current status of the process. The two most common entries in the STAT column are S for sleeping and R for running. A sleeping process is one that isn't currently active. A running process is one that is currently executing on the CPU. Processes may switch between sleeping and running many times every second.







<BR>







<P>The TIME column shows the total amount of system (CPU) time used by the process so far. These numbers tend to be very small for most processes, as they require only a short time to complete. The numbers under the TIME column are a total of the CPU time, not the amount of time the process has been alive.







<BR>







<P>Finally, the NAME column contains the name of the process you are running. This name is usually the command you entered, although some commands start up other processes. These processes are called child processes, and they show up in the ps output as though you had entered them as commands.







<BR>







<P>As a general convention, login shells have a hyphen placed before their name (such as -bash in the preceding output) to help you distinguish the startup shell from any shells you may have started afterwards. Any other shells that appear in the output don't have the hyphen in front of the name, as the following example shows:







<BR>







<PRE>







<FONT COLOR="#000080">$ ps







 PID TTY STAT TIME COMMAND







 46 v01 S 0:01 -bash







 75 v01 S 0:00 phksh







 96 v01 R 0:00 bash







 123 v01 R 0:00 ps</FONT></PRE>







<P>This example shows that the user's startup shell is bash (PID 46) and that the user started up the Korn shell (pdksh, PID 75) and another Bourne shell (bash, PID 96) afterwards. Notice also that the process status, ps, appears in this output (and the previous one) because it is running when you issued the command. The ps command always appears in the output.







<BR>







<P>When a user issues the ps command, that user sees only his own processes. If you issue the ps command when you are logged in as the superuser, you see all the processes on the system because the root login owns everything running. Because this command can produce very long outputs, especially on a system with several users, you may want to pipe the output from the ps command to a page filter (such as more or less) or save the output in a file for further examination. Both commands are shown in the following code:







<BR>







<PRE>







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







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







<P>The ps command has a number of options and arguments, although most system administrators use only a couple of common command line formats. A useful ps option for checking user processes is -u, which adds several columns to the output of the ps command. The following output is from a user (not root) command using this option:







<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 lookup procedure in the /etc/passwd file to convert the user identification number to the proper username).







<BR>







<P>This option also adds the column labeled %CPU, which shows the percentage of CPU time that the process has used 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. If you see a user process that has very high usage, check to make sure it is a valid process and not a runaway that will continue to drain 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, consider paginating the output to make it readable. You also can use the -u option to specify a user's processes by adding the appropriate username. For example, if you are logged in as root and want to see only yvonne's processes, issue the following command:







<BR>







<BR>







<PRE>







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







<P>Most users can issue this command to examine other user's processes, as well. This command lets them find out who is hogging all the CPU time! The -u option also enables the superuser see the processes users are running when they report problems without having to wade through all the system processes as well. Finally, the -u option with a username is handy to help terminate user processes when they are hung or start to run away.







<BR>







<P>Users can see all the processes running on the system (instead of just the processes they started) 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>







⌨️ 快捷键说明

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