Tải bản đầy đủ (.pdf) (18 trang)

Checking and Managing Running Processes

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (239.34 KB, 18 trang )

Checking and Managing
Running Processes
When an executable program starts up, it runs
as a process that is under the management of
your Linux system’s process table. Linux pro-
vides all the tools you need to view and change
the processes running on your system.
The
ps
and
top
commands are great for viewing
information on your running processes. There are
literally dozens of options to
ps
and
top
to help
you view process information exactly the way you
want to. The
pgrep
command can further help
find the process you want.
There are commands such as
nice
and
renice
for raising and lowering processor priority for a
process. You can move processes to run in the
background (
bg


command) or back to the fore-
ground (
fg
command).
Sending signals to a process is a way of changing
its behavior or killing it altogether. Using the
kill
and
killall
commands, you can send signals to
processes by PID or name, respectively. You can
also send other signals to processes to do such
things as reread configuration files or continue
with a stopped process.
To run commands at scheduled times or so they are not tied to your shell
session, you can use the
at
and
batch
commands. To run commands
repetitively at set times, there are the cron and anacron facilities. Or you
can drop scripts (or symbolic links to scripts) into
/etc/cron.hourly
(or
cron.daily
,
cron.weekly
, or
cron.monthly
).

IN THIS CHAPTER
Viewing active
processes with ps
and top
Searching for
processes with pgrep
Adjusting CPU priority
with nice and renice
Moving processes to
the background (bg)
or foreground (fg)
Killing and signaling
processes with kill
and killall
Using at and batch to
run commands
Scheduling commands
to run repeatedly
with cron
82935c09.qxd:Toolbox 10/29/07 1:34 PM Page 169
Listing Active Processes
To see which processes are currently running on a system, most people use the
ps
and
top
commands. The
ps
command gives you a snapshot (in a simple list)
of processes running at the moment. The
top

command offers a screen-oriented,
constantly updated listing of running commands, sorted as you choose (by CPU
use, memory use, UID, and so on).
Viewing Active Processes with ps
Every Linux system (as well as every system derived from Unix, such as BSD, Mac OS X,
and others) includes the
ps
command. Over the years, however, many slightly different
versions of
ps
have appeared, offering slightly different options. Because
ps
dates back
to the first Unix systems, it also supports nonstandard ways of entering some options
(for example, allowing you to drop the dash before an option in some cases).
The different uses of
ps
shown in this chapter will work on Ubuntu and most other
Linux systems. Here are some examples you can run to show processes running for the cur-
rent user (Table 9-1 contains column descriptions of
ps
output):
$ ps List processes of current user at current shell
PID TTY TIME CMD
2552 pts/0 00:00:00 bash
3438 pts/0 00:00:00 ps
$ ps -u chris Show all chris’ running processes (simple output)
PID TTY TIME COMMAND
2678 tty1 0:00 startx
2689 tty1 0:00 xinit

2710 tty1 0:06 gnome-session
...
$ ps -u chris u Show all chris’ running processes (with CPU/MEM)
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
chris 2678 0.0 0.0 4328 852 tty1 S+ Aug14 0:00 /bin/sh startx
chris 2689 0.0 0.1 2408 488 tty1 S+ Aug14 0:00 xinit
chris 2710 0.0 1.1 22016 5496 tty1 S Aug14 0:06 gnome-session
...
$ ps -fu chris Show all chris’ running processes (with PPID)
UID PID PPID C STIME TTY TIME CMD
chris 2678 2645 0 Aug14 tty1 00:00:00 /bin/sh /usr/X11R6/bin/startx
chris 2689 2678 0 Aug14 tty1 00:00:00 xinit /etc/X11/xinit/xinitrc
chris 2710 2689 0 Aug14 tty1 00:00:09 /usr/bin/gnome-session
...
$ ps -Fu chris Show all chris’ running processes (with SZ and PSR)
UID PID PPID C SZ RSS PSR STIME TTY TIME CMD
chris 2678 2645 0 1082 852 0 Aug14 tty1 00:00:00 /bin/sh startx
chris 2689 2678 0 602 488 0 Aug14 tty1 00:00:00 xinit
chris 2710 2689 0 5504 5440 0 Aug14 tty1 00:00:09 gnome-session
...
These examples illustrate some of the processes from a user running a GNOME desktop
session. The first example above shows
ps
alone being run from a Terminal window, so
Chapter 9: Checking and Managing Running Processes
170
82935c09.qxd:Toolbox 10/29/07 1:09 PM Page 170
you only see the processes for the current shell running in that window. Other examples
let you display different information for each process (see later examples for ways of
producing custom output). See Table 9-1 for descriptions of columns displayed by

ps
.
Here are
ps
examples showing output for every process currently running on the system:
$ ps -e Show every running process
PID TTY TIME CMD
1 ? 00:00:01 init
2 ? 00:00:00 migration/0
3 ? 00:00:00 ksoftirqd/0
...
$ ps -el Show every running process, long listing
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
4 S 0 1 0 0 75 0 - 534 - ? 00:00:01 init
1 S 0 2 1 0 -40 - - 0 - ? 00:00:00 migration/0
1 S 0 3 1 0 94 19 - 0 - ? 00:00:00 ksoftirqd/0
...
$ ps -ef Show every running process, full-format listing
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 Aug05 ? 00:00:01 init [5]
root 2 1 0 Aug05 ? 00:00:00 [migration/0]
root 3 1 0 Aug05 ? 00:00:00 [ksoftirqd/0]
...
$ ps -eF Show every running process, extra full-format listing
UID PID PPID C SZ RSS PSR STIME TTY TIME CMD
root 1 0 0 534 556 0 Aug05 ? 00:00:01 init [5]
root 2 1 0 0 0 0 Aug05 ? 00:00:00 [migration/0]
root 3 1 0 0 0 0 Aug05 ? 00:00:00 [ksoftirqd/0]
...
$ ps ax Show every running process, short BSD style

PID TTY STAT TIME COMMAND
1 ? Ss 0:01 init [5]
2 ? S 0:00 [migration/0]
3 ? SN 0:00 [ksoftirqd/0]
...
$ ps aux Show every running process, long BSD style
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 2136 556 ? Ss Aug05 0:01 init [5]
root 2 0.0 0.0 0 0 ? S Aug05 0:00 [migration/0]
root 3 0.0 0.0 0 0 ? SN Aug05 0:00 [ksoftirqd/0]
...
$ ps auwx Show every running process, long BSD style, wide format
$ ps auwwx Show every running process, long BSD style, unlimited width
Some processes start up other processes. For example, a web server (httpd daemon)
will spin off multiple httpd daemons to wait for requests to your web server. You can
view the hierarchy of processes (in a tree view) using various options with
ps
:
$ ps -ejH Show process hierarchy with process/session IDs
PID PGID SID TTY TIME CMD
1 1 1 ? 00:00:01 init
2 1 1 ? 00:00:00 migration/0
171
Chapter 9: Checking and Managing Running Processes
82935c09.qxd:Toolbox 10/29/07 1:09 PM Page 171
2043 2043 2043 ? 00:00:00 sshd
2549 2549 2549 ? 00:00:00 sshd
2551 2549 2549 ? 00:00:00 sshd
2552 2552 2552 pts/0 00:00:00 bash
7760 7760 7760 ? 00:00:00 httpd

7762 7760 7760 ? 00:00:00 httpd
7763 7760 7760 ? 00:00:00 httpd
$ ps axjf Show process hierarchy in BSD-style output
PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND
0 1 1 1 ? -1 Ss 0 0:01 init [5]
1 2 1 1 ? -1 S 0 0:00 [migration/0]
1 2043 2043 2043 ? -1 Ss 0 0:00 /usr/sbin/sshd
2043 2549 2549 2549 ? -1 Ss 0 0:00 \_ sshd: chris [priv]
2549 2551 2549 2549 ? -1 S 500 0:00 | \_ sshd: chris@pts
2551 2552 2552 2552 pts/0 8398 Ss 500 0:00 | \_ -bash
1 7760 7760 7760 ? -1 Ss 0 0:00 /usr/sbin/httpd
7760 7762 7760 7760 ? -1 S 48 0:00 \_ /usr/sbin/httpd
7760 7763 7760 7760 ? -1 S 48 0:00 \_ /usr/sbin/httpd
$ ps -ef --forest Show process hierarchy in forest format
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 Aug05 ? 00:00:01 init [5]
root 2 1 0 Aug05 ? 00:00:00 [migration/0]
root 3 1 0 Aug05 ? 00:00:00 [ksoftirqd/0]
root 2043 1 0 Aug05 ? 00:00:00 /usr/sbin/sshd
root 2549 2043 0 Aug16 ? 00:00:00 \_ sshd: chris [priv]
chris 2551 2549 0 Aug16 ? 00:00:00 | \_ sshd: chris@pts/0
chris 2552 2551 0 Aug16 pts/0 00:00:00 | \_ -bash
root 7760 1 0 18:27 ? 00:00:00 /usr/sbin/httpd
apache 7762 7760 0 18:27 ? 00:00:00 \_ /usr/sbin/httpd
apache 7763 7760 0 18:27 ? 00:00:00 \_ /usr/sbin/httpd
$ pstree Show processes alphabetically in tree format
init-+-Xorg
|-at-spi-registry
|-atd
|-auditd-+-audispd

| `-{auditd}
...
|-sshd-+-sshd---sshd---bash---pstree
| |-sshd---sshd---bash---su---bash
| `-sshd---sshd---bash---su---bash---su---bash---vim
...
The “tree” examples just shown illustrate different ways of displaying the hierarchy
of processes. The output was snipped to compare several of the same processes with
different output. Note that the PPID (Parent Process ID) is the ID of the process that
started each child process shown. The
sshd
processes show a running Secure Shell
Daemon with a user logging in over the network, resulting in a bash shell (and even-
tually a
vim
editor) starting. The
httpd
daemon represents the Apache web server,
with the parent started by the root user and child processes started as the apache user.
The last example shows the
pstree
command, which is specifically used for display-
ing tree views of processes.
172
Chapter 9: Checking and Managing Running Processes
82935c09.qxd:Toolbox 10/29/07 1:09 PM Page 172
If you prefer personalized views of
ps
output, you can select exactly which columns
of data to display with

ps
using the
-o
option. You can then use the
--sort
option to
sort the output by any of those data. Table 9-1 shows available column output and the
options to add to
-o
to have each column print with
ps
.
Table 9-1: Selecting and Viewing ps Column Output
Continued
Option Column Head Description
%cpu %CPU
CPU utilization of process’s lifetime in 00.0 format
%mem %MEM
Percentage of process’s machine’s physical memory
use (resident set size)
args COMMAND
Command with all arguments
bsdstart START
Start time of command started: HH:MM or Mon Day
bsdtime TIME
Total (user and system) CPU time
comm COMMAND
Command name only (no arguments shown)
cp CP
CPU utilization in tenth-of-a-percentage

cputime TIME
Total CPU time in [DD-]HH:MM:SS format
egid EGID
Effective group ID of the process (as integer)
egroup EGROUP
Effective group ID of the process (as name)
etime ELAPSED
Time since process was started, in
[[DD-]HH:]MM:SS format
euid EUID
Effective user ID of the process (as integer)
euser EUSER
Effective user ID of the process (as name)
fgid FGID
File system access group ID (as number)
fgroup FGROUP
File system access group ID (as name)
fname COMMAND
First eight characters of command name
fuid FUID
File system access user ID (as number)
fuser FUSER
File system access user ID (as name)
173
Chapter 9: Checking and Managing Running Processes
82935c09.qxd:Toolbox 10/29/07 1:09 PM Page 173
Table 9-1: Selecting and Viewing ps Column Output (continued)
Option Column Head Description
lstart STARTED
Date and time the command started

nice NI
Nice value, from 19 (nicest) to –20 (CPU hog)
pgid PGID
Process group ID of process
pid PID
Process ID number of process
ppid PPID
Parent process ID of process
psr PSR
Processor process is assigned to (first CPU is 0)
rgid RGID
Real group ID (as number)
rgroup RGROUP
Real group (as name)
rss RSS
Non-swapped physical memory (resident set size)
in KB
rtprio RTPRIO
Real-time priority
ruid RUID
Real user ID (as number)
ruser RUSER
Real user (as name)
s S
One-character state display (D:sleep, no interrupt;
R:running; S:sleep, can interrupt; T:stopped;
W:paging; X:dead; Z:zombie)
sess SESS
Session ID of session leader
sgi_p P

Processor that process is currently running on
size SZ
Rough amount of swap space needed if process were
to swap out
start STARTED
Time command started: HH:MM:SS or Month Day
start_time START
Time command started: HH:MM or MonthDay
stat STAT
Multi-character state: One-character “s” state plus
other state characters (<:High priority; N:Low prior-
ity; L:Has pages locked in memory; s:Is session leader;
l:Multi-threaded; +:in foreground process group)
sz SZ
Size of process’s core image (physical pages)
174
Chapter 9: Checking and Managing Running Processes
82935c09.qxd:Toolbox 10/29/07 1:09 PM Page 174
Table 9-1: Selecting and Viewing ps Column Output (continued)
Note that some values that are meant to print user names may still print numbers (UIDs)
instead, if the name is too long to fit in the given space.
Using a comma-separated list of column options, you can produce your custom output.
Here are some examples of custom views of running processes:
$ ps -eo ppid,user,%mem,size,vsize,comm --sort=-size Sort by mem use
PPID USER %MEM SZ VSZ COMMAND
1 root 27.0 68176 84264 yum-updatesd
$ ps -eo ppid,user,bsdstart,bsdtime,%cpu,args --sort=-%cpu Sort by CPU use
PPID USER START TIME %CPU COMMAND
1 root Jul 30 44:20 27.1 /usr/bin/python /usr/sbin/yum-updatesd
$ ps -eo ppid,user,nice,cputime,args --sort=-nice Sort by low priority

PPID USER NI TIME COMMAND
1 root 19 00:44:26 /usr/bin/python /usr/sbin/yum-updatesd
$ ps -eo ppid,user,stat,tname,sess,cputime,args --sort=user Sort by user
PPID USER STAT TTY SESS TIME COMMAND
1 avahi Ss ? 2221 00:00:07 avahi-daemon: running [example.net]
Here are a few other extraneous examples of the ps command:
$ ps -C httpd Display running httpd processes
PID TTY TIME CMD
1493 ? 00:00:00 httpd
1495 ? 00:00:00 httpd
Note that you need to install an HTTP server, such as Apache, to run an httpd process.
$ ps -p 5413 -o pid,ppid,bsdtime,args Display info for PID 5413
PID PPID TIME COMMAND
5413 1 0:08 gpm -m /dev/input/mice -t exps2
$ ps -U chris,francois -o pid,ruser,tty,stat,args See info for 2 users
PID RUSER TT STAT COMMAND
1010 chris pts/0 Ss -bash
5951 francois pts/1 Ss+ /bin/bash
Watching Active Processes with top
If you want to see the processes running on your system on an ongoing basis, you can use the
top
command. The
top
command runs a screen-oriented view of your running processes
Option Column Head Description
tname TTY
Controlling tty (terminal)
user USER
Effective user ID of process (as name)
vsize VSZ

Process’s virtual memory (1024-byte units)
175
Chapter 9: Checking and Managing Running Processes
82935c09.qxd:Toolbox 10/29/07 1:09 PM Page 175

×