📄 ch40.htm
字号:
<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>
-->
<UL>
<LI><A HREF="#Heading1">- 40 -</A>
<UL>
<LI><A HREF="#Heading2">Processes</A>
<UL>
<LI><A HREF="#Heading3">Processes</A>
<UL>
<LI><A HREF="#Heading4">Types of Processes</A>
</UL>
<LI><A HREF="#Heading5">Using the ps Command</A>
<UL>
<LI><A HREF="#Heading6">ps Command Output</A>
<LI><A HREF="#Heading7">Login Shells</A>
<LI><A HREF="#Heading8">For the Superuser</A>
<LI><A HREF="#Heading9">Useful ps Options</A>
</UL>
<LI><A HREF="#Heading10">NOTE</A>
<LI><A HREF="#Heading11">Using kill</A>
<UL>
<LI><A HREF="#Heading12">killing Child Processes</A>
</UL>
<LI><A HREF="#Heading13">WARNING</A>
<UL>
<LI><A HREF="#Heading14">killing Rights</A>
</UL>
<LI><A HREF="#Heading15">Summary</A>
</UL>
</UL>
</UL>
<P>
<HR SIZE="4">
<H2 ALIGN="CENTER"><A NAME="Heading1<FONT COLOR="#000077">- 40 -</FONT></H2>
<H2 ALIGN="CENTER"><A NAME="Heading2<FONT COLOR="#000077">Processes</FONT></H2>
<P><I>by Tim Parker</I></P>
<P>IN THIS CHAPTER</P>
<UL>
<LI>Processes
<P>
<LI>Using the ps Command
<P>
<LI>Using kill
</UL>
<P>Everything that runs on a Linux system is a process--every user task, every system
daemon--everything is a process. Knowing how to manage the processes running on your
Linux system is an important (indeed even critical) aspect of system administration.
This chapter looks at processes in some detail. In this chapter you will see:
<UL>
<LI>How to find out what processes are running
<P>
<LI>How to determine when a process is hogging the system
<P>
<LI>How to find out which processes are locked up
<P>
<LI>How to terminate a process
<P>
<LI>How to properly manage processes
</UL>
<P>In the course of discussing processes, we don't bother with 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, we'll look at the nitty-gritty
aspects of process control that you need in order to keep your system running smoothly.</P>
<P>You may come across the terms process and job used when dealing with multitasking
operating systems. For most purposes, both terms are correct. However, a job is usually
a process started by a shell (and may involve many processes), while a process is
a single entity that is executing. To be correct, we'll use the term process throughout.
<H3 ALIGN="CENTER"><A NAME="Heading3<FONT COLOR="#000077">Processes</FONT></H3>
<P>A formal definition of a process is that it is a single program running in its
own virtual address space. This means that everything running under Linux is a process.
This is compared to a job, which may involve several commands executing in series.
Alternatively, a single command line issued at the shell prompt may involve more
than one process, especially when pipes or redirection are involved. For example,
the command<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">nroff -man ps.1 | grep kill | more
</FONT></PRE>
<P>will start three processes, one for each command.
<H4 ALIGN="CENTER"><A NAME="Heading4<FONT COLOR="#000077">Types of Processes</FONT></H4>
<P>There are several types of processes involved with the Linux operating system.
Each has its own special features and attributes. These are the processes involved
with Linux:
<UL>
<LI>Interactive processes: A process initiated from (and controlled by) a shell.
Interactive processes may be in foreground or background.
<P>
<LI>Batch processes: Processes that are not associated with a terminal but are submitted
to a queue to be executed sequentially.
<P>
<LI>Daemon processes: Processes usually initiated when Linux boots and that run in
the background until required.
</UL>
<H3 ALIGN="CENTER"><A NAME="Heading5<FONT COLOR="#000077">Using the ps Command</FONT></H3>
<P>The easiest method of finding out what processes are running on your system is
to use the <TT>ps</TT> (process status) command. The <TT>ps</TT> command has a number
of options and arguments, although most system administrators use only a couple of
common command-line formats. We can start by looking at the basic usage of the <TT>ps</TT>
command, and then examine some of the useful options.</P>
<P>The <TT>ps</TT> 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.</P>
<P>When you are logged in as a normal system user (in other words, any login but
root) and issue the <TT>ps</TT> command on the command line by itself, it displays
information about every process you are running. For example, you might see the following
output when you issue this command:
<PRE><FONT COLOR="#0066FF">$ ps
PID TTY STAT TIME COMMAND
41 v01 S 0:00 -bash
134 v01 R 0:00 ps
</FONT></PRE>
<H4 ALIGN="CENTER"><A NAME="Heading6<FONT COLOR="#000077">ps Command Output</FONT></H4>
<P>The output of the <TT>ps</TT> command is always organized in columns. Every process
on the system has to have a unique identifier so Linux can tell which processes it
is working with. Linux handles processes by assigning a unique number to each process,
called the "Process ID" number (or PID). PIDs start at zero when the system
is booted and increment by one for each process run, up to some system-determined
number (such as 65,564), at which point it starts numbering from zero again, ignoring
those that are still active. 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. When you are working with processes (such as terminating them), you must
use the PID.</P>
<P>The <TT>TTY</TT> column in the <TT>ps</TT> command output shows you which terminal
the process was started from. If you are logged in as a user, this will usually be
your terminal or console window. If you are running on multiple console windows,
you will see all the processes you started in every window displayed.</P>
<P>The <TT>STAT</TT> column in the <TT>ps</TT> command output shows you the current
status of the process. The two most common entries in the status column are <TT>S</TT>
for sleeping and <TT>R</TT> for running. A running process is one that is currently
executing on the CPU. A sleeping process is one that isn't currently active. Processes
may switch between sleeping and running many times every second.</P>
<P>The <TT>TIME</TT> 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 because they
require only a short time to complete. The numbers under the <TT>TIME</TT> column
are a total of the CPU time, not the amount of time the process has been alive.</P>
<P>Finally, the <TT>COMMAND</TT> column contains the name of the command line you
are running. This is usually the command line you used, although some commands start
up other processes. These are called "child" processes, and they show up
in the <TT>ps</TT> output as if you had entered them as commands.
<H4 ALIGN="CENTER"><A NAME="Heading7<FONT COLOR="#000077">Login Shells</FONT></H4>
<P>As a general convention, a login shell has a hyphen placed before its name (such
as <TT>-bash</TT> in the above output) to help you distinguish the startup shell
from any shells you may have started afterwards. Any other shells that appear in
the output do not have the hyphen in front of the name, as the following example
shows:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">$ ps
PID TTY STAT TIME COMMAND
46 v01 S 0:01 -bash
75 v01 S 0:00 pdksh
96 v01 R 0:00 bash
123 v01 R 0:00 ps
</FONT></PRE>
<P>This output shows that the user's startup shell is <TT>bash</TT> (PID 46), and
that he or she started up the Korn shell (<TT>pdksh</TT>, PID 75) and another Bourne
shell (<TT>bash</TT>, PID 96) afterwards.</P>
<P>Notice in the preceding outputs that the command that actually showed you the
process status, <TT>ps</TT>, appears on the output because it was running when you
issued the command. The <TT>ps</TT> command always appears on the output.
<H4 ALIGN="CENTER"><A NAME="Heading8<FONT COLOR="#000077">For the Superuser</FONT></H4>
<P>When normal users issue the <TT>ps</TT> command, they see only their own processes.
If you issue the <TT>ps</TT> command when you are logged in as the superuser (usually
root, although you can change the name), you will see all the processes on the system,
because the root login owns everything running. This can produce very long outputs,
especially on a system with several users, so you probably want to pipe the output
from the <TT>ps</TT> command to a page filter (such as <TT>more</TT> or <TT>less</TT>),
or save the output in a file for further examination. Both commands are shown here:<FONT
COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">ps | more
ps > /tmp/ps_file
</FONT></PRE>
<H4 ALIGN="CENTER"><A NAME="Heading9<FONT COLOR="#000077">Useful ps Options</FONT></H4>
<P>A useful <TT>ps</TT> option for checking user processes is <TT>-u</TT>, which
adds several columns to the output of the <TT>ps</TT> command. The output from a
user (not root) command using this option looks like this:<FONT COLOR="#0066FF"></FONT>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -