LPI Linux Certification in a Nutshell (18 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
7.31Mb size Format: txt, pdf, ePub
Using the tee Command

Sometimes you’ll want to run a program and send its output to a
file while at the same time viewing the output on the screen. The
tee
utility is helpful in this
situation.

The xargs Command

Sometimes you need to pass a list of items to a command that is
longer than your shell can handle. In these situations, the
xargs
command can be used to break
down the list into smaller sublists.

Name

tee

Syntax
tee [
options
]
files
Description

Read from standard input and write both to one or more
files
and to standard output (analogous
to a tee junction in a pipe).

Option
-a

Append to
files
rather than
overwriting them.

Example

Suppose you’re running a pipeline of commands
cmd1
,
cmd2
, and
cmd3
:

$
cmd1 | cmd2 | cmd3 > file1

This sequence puts the ultimate output of the pipeline into
file1
. However, you may also be interested in
the intermediate result of
cmd1
. To create a
new
file_cmd1
containing those results, use
tee
:

$
cmd1 | tee file_cmd1 | cmd2 | cmd3 > file1

The results in
file1
will be the same as
in the original example, and the intermediate results of
cmd1
will be placed in
file_cmd1
.

Name

xargs

Syntax
xargs [
options
] [
command
] [
initial-arguments
]
Description

Execute
command
followed by its
optional
initial-arguments
and append
additional arguments found on standard input. Typically, the
additional arguments are filenames in quantities too large for a
single command line.
xargs
runs
command
multiple times to exhaust all
arguments on standard input.

Frequently used options
-n
maxargs

Limit the number of additional arguments to
maxargs
for each invocation of
command
.

-p

Interactive mode. Prompt the user for each execution of
command
.

Example

Use
grep
to search a long list of files,
one by one, for the word “linux”:

$
find / -type f | xargs -n 1 grep -H linux

find
searches for normal files
(
-type f
) starting at the root directory.
xargs
executes
grep
once
for each of them due to the
-n 1
option.
grep
will print the matching line preceded by
the filename where the match occurred (due to the
-H
option).

Objective 5: Create, Monitor, and Kill Processes

This Objective looks at the management of
processes
. Just as file management is a fundamental
system administrator’s function, the management and control of processes
is also essential for smooth system operation. In most cases, processes
will live, execute, and die without intervention from the user because
they are automatically managed by the kernel. However, there are times
when a process will die for some unknown reason and need to be restarted.
Or a process may “run wild” and consume system resources, requiring that
it be terminated. You will also need to instruct running processes to
perform operations, such as rereading a configuration file.

Processes

Every program, whether it’s a command, application, or
script, that runs on your system is a
process
. Your
shell is a process, and every command you execute from the shell starts
one or more processes of its own (referred to as
child processes
). Attributes and
concepts associated with these processes include:

Lifetime

A process lifetime is defined by the length of time
it takes to execute (while it “lives”). Commands with a short
lifetime such as
ls
will execute for a very
short time, generate results, and terminate when complete. User
programs such as web browsers have a longer lifetime, running for
unlimited periods of time until terminated manually. Long-lifetime
processes include server daemons that run continuously from system
boot to shutdown. When a process terminates, it is said to
die
(which is why the program used to
manually signal a process to stop execution is called
kill
; succinct, though
admittedly morbid).

Process ID (PID)

Every process has a number assigned to it when it
starts. PIDs are integer numbers unique among all running
processes.

User ID (UID) and Group ID (GID)

Processes must have associated privileges, and a
process’s UID and GID are associated with the user who started the
process. This limits the process’s access to objects in the
filesystem.

Parent process

The first process started by the kernel at system start time
is a program called
init
. This process has PID 1
and is the ultimate parent of all other processes on the system.
Your shell is a descendant of
init
and the
parent process to commands started by the shell, which are its
child
processes, or
subprocesses
.

Parent process ID (PPID)

This is the PID of the process that created the
process in question.

Environment

Each process holds a list of variables and their associated
values. Collectively, this list is known as the
environment
of the process, and the variables
are called
environment variables
. Child
processes inherit their environment settings from the parent
process unless an alternative environment is specified when the
program is executed.

Current working directory

The default directory associated with each process.
The process will read and write files in this directory unless
they are explicitly specified to be elsewhere in the
filesystem.

On the Exam

The
parent/child relationship of the processes on a Linux
system is important. Be sure to understand how these relationships
work and how to view them. Note that the
init
process always has PID 1 and is the ultimate ancestor of all system
processes (hence the nickname “mother of all processes”). Also
remember the fact that if a parent process is killed, all its children
(subprocesses) die as well.

Process Monitoring

At any time, there could be tens or even hundreds of
processes running together on your Linux system. Monitoring these
processes is done using three convenient utilities:
ps
,
pstree
, and
top
.

Signaling Active Processes

Each process running on your system listens for
signals
, simple messages sent to the process either
by the kernel or by a user. The messages are sent through inter-process
communication. They are single-valued, in that they don’t contain
strings or
command
-
like constructs.
Instead, signals are numeric integer messages, predefined and known by
processes. Most have an implied action for the process to take. When a
process receives a signal, it can (or may be forced to) take
action.

For example, if you are executing a program from the command line
that appears to hang, you may elect to type
Ctrl-C to abort the program. This action actually sends an
SIGINT
(interrupt signal) to the process, telling
it to stop running.

There are more than 32 signals defined for normal process use in
Linux. Each signal has a name and a number (the number is sent to the
process; the name is only for our convenience). Many signals are used by
the kernel, and some are useful for users.
Table 6-7
lists popular signals
for interactive use.

Table 6-7. Frequently used interactive signals

Signal name
[
a
]

Number

Meaning and use

HUP

1

Hang up. This signal is sent
automatically when you log out or disconnect a modem. It is also
used by many daemons to cause the configuration file to be
reread without stopping the daemon process. Useful for things
like an
httpd
server that normally reads
its configuration file only when the process is started. A
SIGHUP
signal will force it to reread the
configuration file without the downtime of restarting the
process.

INT

2

Interrupt; stop running. This signal
is sent when you type Ctrl-C.

KILL

9

Kill; stop unconditionally and
immediately. Sending this signal is a drastic measure, as it
cannot be ignored by the process. This is the “emergency kill”
signal.

TERM

15

Terminate, nicely if possible. This
signal is used to ask a process to exit gracefully, after its
file handles are closed and its current processing is
complete.

TSTP

20

Stop executing, ready to continue.
This signal is sent when you type
Ctrl-Z. (See the later section
“Shell Job Control”
for more
information.)

CONT

18

Continue execution. This signal is
sent to start a process stopped by
SIGTSTP
or
SIGSTOP
. (The shell sends this signal
when you use the
fg
or
bg
commands after stopping a process with
Ctrl-Z
.)

[
a
]
Signal names often will be specified with a SIG
prefix. That is, signal HUP is the same as signal
SIGHUP.

As you can see from
Table 6-7
, some signals are
invoked by pressing well-known key combinations such as Ctrl-C and
Ctrl-Z. You can also use the
kill
command to send any message to a
running process. The
kill
command is implemented
both as a shell built-in command and as a standalone binary command. For
a complete list of signals that processes can be sent, refer to the file
/usr/include/bits/signum.h
on your
Linux install, which normally is installed with the
glibc-headers
package.

Terminating Processes

Occasionally, you’ll find a system showing symptoms of
high CPU load or one that runs out of memory for no obvious reason. This
often means an application has gone out of control on your system. You
can use
ps
or
top
to identify processes that may be
having a problem. Once you know the PID for the process, you can use the
kill
command to stop the process nicely with
SIGTERM
(
kill
-15
PID
), escalating the signal to
higher strengths if necessary until the process terminates.

Note

Occasionally you may see a process displayed by
ps
or
top
that is listed as
a
zombie
. These are processes that
are stuck while trying to terminate and are appropriately said to be
in the
zombie state
. Just as in the cult classic
film
Night of the Living Dead
, you can’t kill
zombies, because they’re already dead!

If you have a recurring problem with zombies, there may be a bug
in your system software or in an application.

Killing a process will also kill all of its child processes. For
example, killing a shell will kill all the processes initiated from that
shell, including other shells.

Shell Job Control

Linux and most modern Unix systems offer
job
control
, which is the ability of your shell (with support of
the kernel) to stop and restart executing commands, as well as place
them in the
background
where they can be
executed. A program is said to be in the
foreground
when it is attached to
your terminal. When executing in the background, you have no input to
the process other than sending it signals. When a process is put in the
background, you create a
job
. Each job is assigned
a job number, starting at 1 and numbering sequentially.

The basic reason to create a background process is to keep your
shell session free. There are many instances when a long-running program
will never produce a result from standard output or standard error, and
your shell will simply sit idle waiting for the program to finish.
Noninteractive programs can be placed in the
background
by adding a
&
character to the command. For example,
if you start
firefox
from the command
line, you don’t want the shell to sit and wait for it to terminate. The
shell will respond by starting the web browser in the background and
will give you a new command prompt. It will also issue the job number,
denoted in square brackets, along with the PID. For example:

$
/usr/bin/firefox &
[1] 1748

Here,
firefox
is started as a
background process. Firefox is assigned to job 1 (as denoted by
[1]
), and is assigned PID
1748
. If you start a program and forget the
&
character, you can still put it
in the background by first typing Ctrl-Z to stop it:

^Z
[1]+ Stopped firefox

Then, issue the
bg
command to restart the job
in the background:

$
bg
[1]+ /usr/bin/firefox &

When you exit from a shell with jobs in the background, those
processes may die. The utility
nohup
can be used to protect the
background processes from the hangup signal
(
SIGHUP
) that it might otherwise receive when the
shell dies. This can be used to simulate the detached behavior of a
system daemon.

Putting interactive programs in the background can be quite
useful. Suppose you’re logged into a remote Linux system, running Emacs
in text mode. Rather than exit from the editor when you need to drop
back to the shell, you can simply press
Ctrl-Z
. This stops
Emacs, puts it in the background, and returns you to a command prompt.
When you are finished, you resume your Emacs session with the
fg
command, which puts your stopped job back into
the foreground.

Background jobs and their status can be listed by issuing the
jobs
command. Stopped jobs can be
brought to the foreground with the
fg
command and
optionally placed into the background with the Ctrl-Z and
bg
sequence.

Other books

Love's Rescue by Tammy Barley
Kiss by Ted Dekker
Anticipation by Tanya Moir
Wildcard by Ken McClure
Deadlocked 7 by Wise, A.R.
The Box Garden by Carol Shields
Collected Poems by Sillitoe, Alan;
Secret Weapons by Zilpha Keatley Snyder
Decay Inevitable by Conrad Williams