LPI Linux Certification in a Nutshell (19 page)

Read LPI Linux Certification in a Nutshell Online

Authors: Adam Haeder; Stephen Addison Schneiter; Bruno Gomes Pessanha; James Stanger

Tags: #Reference:Computers

BOOK: LPI Linux Certification in a Nutshell
2.12Mb size Format: txt, pdf, ePub
Name

ps

Syntax
ps [
options
]
Description

This command generates a one-time snapshot of the
current processes on standard output.

Frequently used options
-a

Show processes that are owned by other users and
attached to a terminal. Normally, only the current user’s
processes are shown.

-f

“Full-format” listing. This option prints command
arguments in addition to the command itself.

-l

Long format, which includes priority, parent PID, and
other information.

-u

User format, which includes usernames and the start time
of processes.

-w

Wide output format, used to eliminate the default output
line truncation. Specify it twice (
-ww
)
for unlimited width.

-x

Include processes without controlling terminals. Often
needed to see daemon processes and others not started from a
terminal session.

-C
cmd

Display instances of command name
cmd
.

-U
user

Display processes owned by username
user
.

Examples

Simply entering the
ps
command with no
options will yield a brief list of processes owned by you and
attached to your terminal:

$
ps

Use the
-a, -u
, and
-x
options to include processes owned by others
and not attached to terminals as well as to display them in the
“user” mode. The command is valid with or without the dash:

$
ps -aux
$
ps aux

In this case, the dash is optional. However, certain
ps
options require the dash. (See the manpage
for details.)

If you are interested in finding process information on a
particular command, use the
-C
option. This
command displays all web server processes:

$
ps u -C httpd

You’ll note that the
-C
option
requires
the dash, but the
u
option won’t work with it if a dash is
included. This confusion exists because the
ps
command as implemented on Linux understands options in three
differing forms:

Unix98 options

These may be grouped and must be preceded by a
dash.

BSD options

These may be grouped and must
not
be used with a dash.

GNU long options

These options are preceded by
two dashes.

Note

The Linux
ps
tries to be compatible
with
ps
from various other systems. How it
interprets various command-line options, which determines how
compatible it is with other versions of
ps
,
can be controlled by setting
I_WANT_A_BROKEN_PS
,
PS_PERSONALITY
, and various other
environment variables. See the
ps
manpage for
details.

All of these option types may be freely intermixed. Instead of
the
-C
option, you may wish to use
ps
with other options that you usually use and
pipe the output to
grep
, searching for process
names, PIDs, or anything else you know about the process:

$
ps aux | grep httpd

In this case, the result would be the same list of
httpd
servers, as well as the
grep
command itself.

Name

pstree

Syntax
pstree [
options
] [
pid
|
user
]
Description

This command displays a hierarchical list of processes
in a tree format.
pstree
is very handy for
understanding how
parent/child process relationships are set up.

If the
PID is specified, the displayed tree is rooted at that
process. Otherwise, it is rooted at the
init
process, which has PID 1. If
user
(a valid username) is specified,
trees for all processes owned by
user
are
shown. The tree is represented using characters that appear as
lines, such as
|
for vertical
lines and
+
for intersections
(VT100 line-drawing characters, displayed as solid lines by most
terminals, are optional). The output looks similar to this:

httpd-+-httpd
|-httpd
|-httpd
|-httpd
'-httpd

By default, visually identical branches of the tree are merged
to reduce output. Merged lines are preceded by a count indicating
the actual number of similar processes. The preceding example is
normally displayed on a single line:

httpd---5*[httpd]

This behavior can be turned off with the
-c
option.

Frequently used options
-a

Display command-line arguments used to launch
processes.

-c

Disable the compaction of identical subtrees.

-G

Use the VT100 line-drawing characters instead of plain
characters to display the tree. This yields a much more
pleasing display but may not be appropriate for printing or
paging programs.

-h

Highlight the ancestry of the current process (usually
the shell). The terminal must support highlighting for this
option to be meaningful.

-n

The default sort order for processes with the same
parent is alphanumerically by name. This option changes this
behavior to a numeric sort by PID.

-p

Include PIDs in the output.

Example

Display a process tree including PIDs:

#
pstree -p
init(1)-+-atd(468)
|-bdflush(5)
|-crond(454)
|-httpd(440)-+-httpd(450)
| |-httpd(451)
| |-httpd(452)
| |-httpd(453)
| |-httpd(455)
| |-httpd(456)
| |-httpd(457)
| '-httpd(458)
|-keventd(2)
|-kjournald(7)
|-klogd(335)
|-ksoftirqd_CPU0(3)
|-kswapd(4)
|-kupdated(6)
|-login(475)---bash(478)---pstree(518)
|-sendmail(420)
|-sshd(385)
|-syslogd(330)
'-xinetd(402)
Name

top

Syntax
top [
options
]
Description

The
top
command offers output
similar to
ps
, but in a continuously updated
display. This is useful for situations in which you need to watch
the status of one or more processes or to see how they are using
your system.

In addition, a header of useful uptime, load, CPU status, and
memory information is displayed. By default, the process status
output is generated with the most CPU-intensive processes at the top
of the listing (and is named for the “top” processes). To format the
screen,
top
must understand how to control the
terminal display. The type of terminal (or terminal window) in use
is stored in the environment variable
TERM
. If this variable is not set or
contains an unknown terminal type,
top
may not
execute.

Popular command-line options

Dashes are optional in
top
options:

-b

Run in batch mode. This is useful for sending output
from
top
to other programs or to a file.
It executes the number of iterations specified with the
-n
option and terminates. This option is
also useful if
top
cannot display on the
terminal type you are using.

-d
delay

Specify the
delay
in seconds
between screen updates. The default is five seconds.

-i

Ignore idle processes, listing only the “interesting”
ones taking system resources.

-n
num

Display
num
iterations and
then exit, instead of running indefinitely.

-q

Run with no delay. If the user is the superuser, run
with highest possible priority. This option causes
top
to update continuously and will
probably consume any idle time your CPU had. Running
top -q
as superuser will seriously affect
system performance and is not recommended.

-s

Run in secure mode. Some of
top
’s
interactive commands can be dangerous if running as the
superuser. This option disables them.

Frequently used interactive options

Once
top
is running interactively, it can
be given a number of commands via the keyboard to change its
behavior. These commands are single-key commands, some of which
cause
top
to prompt for input:

spacebar

Refresh the screen.

h

Generate a help screen.

k

Kill a process. You will be prompted for the PID of the
process and the signal to send it. (The default signal is 15,
SIGTERM
.) See
Terminating Processes
.

n

Change the number of processes to show. You will be
prompted to enter an integer number. The default is 0, which
indicates that the screen should be filled.

q

Quit the program.

r

Change the priority of a process
(
renice
). You will be prompted for the
PID of the process and the value to nice it to (see
nice
and
renice
in
Objective 6: Modify Process Execution Priorities
).
Entering a positive value causes a process to lose priority.
If the superuser is running
top
, a
negative value may be entered, causing a process to get a
higher than normal priority. This command is not available in
secure mode.

s

Change the delay in seconds between updates. You will be
prompted for the delay value, which may include fractions of
seconds (e.g., 0.5).

Example 1

Simply executing
top
without options
gives a full status display updated every five seconds:

$
top

Use the
q
command to quit.

Example 2

To run
top
with a faster refresh rate,
use the interval option, specified here with a one-second
refresh:

$
top -d 1
Example 3

You may wish to use
top
to log its output
to a file. Use the
-b
(batch) option for this
purpose. In this batch example, the
-i
option
eliminates idle processes, the
-n
option, with
its argument, indicates five iterations, and the
-d
option indicates a one-second interval.
Results will be redirected to
file1
. This
command will take five seconds to execute and does not use the
optional dashes:

$
top –bi -n 5 -d 1 > file1

The single-key interactive commands can be used when
top
is running interactively. For example, if
you type the
h
command,
top
displays a help screen. If you enter the
n
command,
top
prompts you
for the number of lines you wish to display.

Using
top
to change the
nice
(priority modifier) value for a process is
discussed in
Objective 6: Modify Process Execution Priorities
.

Other books

The Never-Open Desert Diner by James Anderson
The Rain in Spain by Amy Jo Cousins
Finding Absolution by Carol Lynne
Sister Girls 2 by Angel M. Hunter
The Fourth Horseman by Sarah Woodbury
How I Live Now by Meg Rosoff