LPI Linux Certification in a Nutshell (39 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.01Mb size Format: txt, pdf, ePub
Objective 103.5: Create, Monitor, and Kill Processes
Concepts
  • Processes have:

    • A lifetime

    • A PID

    • A UID

    • A GID

    • A parent process

    • An environment

    • A current working directory

Monitoring commands
ps

Generate a one-time snapshot of the current processes on
standard output.

pstree

Display a hierarchical list of processes in a tree
format.

top

Generate a continuous, formatted, real-time process
activity display on a terminal or in a terminal window.

Signaling processes
  • Processes listen for
    signals
    sent by the kernel or users using the
    kill
    command:

    kill
    -
    sigspec
    [
    pids
    ]

    Send
    sigspec
    to
    pids
    .

  • The
    killall
    command is used to send
    signals to processes by program name instead of PID.

  • Common
    kill
    signals are listed in
    Table 10-6
    .

Table 10-6. Common signals

Signal

Number

Meaning

HUP

1

Hangup, reread
configuration.

INT

2

Interrupt, stop
running.

KILL

9

Exit immediately.

TERM

15

Terminate nicely.

TSTP

18

Stop executing.

Shell job control

Shells can run processes in the
background
,
where they execute on their own, or in the
foreground
, attached to a terminal. Each process
handled in this way is known as a
job
. Jobs are
manipulated using job control commands:

bg
[
jobspec
]

Place
jobspec
in the background
as if it had been started with
&
.

fg
[
jobspec
]

Place
jobspec
in the
foreground, making it the current job.

jobs
[
jobspecs
]

List
jobspecs
on standard
output.

nohup
[
command
] &

Execute
command
, detach it from
the terminal, and allow it to continue running after the user
logs out.

Objective 103.6: Modify Process Execution Priorities
Concepts
  • A process’s
    execution priority
    is managed by the kernel.

  • You can bias the execution priority by specifying a
    nice number
    in the range of –20 to +19
    (default is 0).

  • Positive nice numbers reduce priority; negative nice numbers
    increase priority and are reserved for the superuser.

Commands
nice
-
adjustment
[
command
]

Apply nice number
adjustment
to
the process created to run
command
.

renice
[+|-]
nicenumber
targets

Alter the
nicenumber
, and thus
the scheduling priority, of one or more running
target
processes.

Objective 103.7: Search Text Files Using Regular
Expressions
Concepts
  • Regular expressions
    are used to
    match text. The term is used to describe the loosely defined
    text-matching language as well as the patterns themselves. A
    regular expression is often called a
    regex
    or
    a
    regexp
    .

  • Regular expressions are made up of
    metacharacters
    (with special
    meaning) and
    literals
    (everything that is not
    a metacharacter).

  • The backslash character (
    \
    ) turns off (escapes) the special
    meaning of the character that follows, turning metacharacters into
    literals. For nonmetacharacters, it often turns on some special
    meaning.

Position anchors

The operators in
Table 10-7
match line
position.

Table 10-7. Regular expression position anchors

Regular
expression

Description

^

Match the beginning of a
line.

$

Match the end of a
line.

\<
\>

Match word boundaries. Word
boundaries are defined as whitespace, start of a line, end of
a line, or punctuation marks. The backslashes are required and
enable this interpretation of
<
and
>
.

Character sets

The operators in
Table 10-8
match
text.

Table 10-8. Regular expression character sets

Regular expression

Description

[
abc
]
[
a-z
]

Match any single character from
among listed characters (
abc
) or from
among the characters comprising a range
(
a–z
).

[^
abc
]
[^
a-z
]

Match any single character not among
listed characters or ranges.

.

Match any single character except a
newline.

Modifiers

The operators in
Table 10-9
modify the way other
operators are interpreted.

Table 10-9. Regular expression modifiers

Basic regular
expression

Extended
regular
expression

Description

*
*

Match zero or more of the character
that precedes it.

\?
?

Match zero or one instance of the
preceding
regex
.

\+
+

Match one or more instances of the
preceding
regex
.

\{
n
,
m
\}
{
n
,
m
}

Match a range of occurrences of the
single character or regex that precedes this construct.
\{
n
\}
matches
n
occurrences,
\{
n
,\}
matches at least
n
occurrences, and
\{
n
,
m
\}
matches any number of occurrences
between
n
and
m
, inclusively.

\|
|

Match the character or expression to
the left or right of the vertical bar.

\(
regex
\)
(
regex
)

Matches
regex
, but it can be modified as a
whole and used in back-references. (
\1
expands to the contents of the
first
\(\)
and so on up to
\9
.)

Commands
  • Many commands support the regular expression syntax, but the
    most commonly used is the command
    grep
    , which
    is designed to display lines from a file or files matching a given
    regular expression.

  • There are multiple ways to call
    grep
    to
    change its behavior:

    grep

    Treat the pattern as a basic regular
    expression.

    egrep

    Treat the pattern as an extended regular expression.
    Same as
    grep –E
    .

    fgrep

    Treat the pattern as a list of fixed strings, any of
    which may be matched. Same as
    grep
    –F
    .

Objective 103.8: Perform Basic File Editing Operations Using
vi
Subcommands
  • Start
    vi
    with
    vi
    file1
    [
    file2
    [...]]
    . See
    Table 10-10
    .

    Table 10-10. Basic vi editing commands

    Command

    Description

    Esc

    Exit insert mode and put the
    editor into command mode.

    h
    or
    left arrow

    Move left one
    character.

    j
    or
    down arrow

    Move down one
    line.

    k
    or
    up
    arrow

    Move up one line.

    l
    or
    right arrow

    Move right one
    character.

    H

    Move to the top of the
    screen.

    L

    Move to the bottom of the
    screen.

    G

    Move to the end of the
    file.

    W

    Move forward one
    word.

    B

    Move backward one
    word.

    0
    (zero)

    Move to the beginning of the
    current line.

    ^

    Move to the first nonwhitespace
    character on the current line.

    $

    Move to the end of the current
    line.

    Ctrl-B

    Move up (back) one
    screen.

    Ctrl-F

    Move down (forward) one
    screen.

    i

    Insert at the current cursor
    position.

    I

    Insert at the beginning of the
    current line.

    a

    Append after the current cursor
    position.

    A

    Append to the end of the current
    line.

    o

    Start a new line after the
    current line.

    O

    Start a new line before the
    current line.

    r

    Replace the character at the
    current cursor position.

    R

    Start replacing (overwriting) at
    the current cursor position.

    x

    Delete the character at the
    current cursor position.

    X

    Delete the character immediately
    before (to the left) of the current cursor
    position.

    s

    Delete the character at the
    current cursor position and go into insert mode. (This is
    the equivalent of the combination
    xi
    .)

    S

    Delete the contents of the
    current line and go into insert mode.

    d
    X

    Given a movement command
    X
    , cut (delete) the appropriate
    number of characters, words, or lines from the current
    cursor position.

    dd

    Cut the entire current
    line.

    D

    Cut from the current cursor
    position to the end of the line. (This is equivalent to
    d$
    .)

    c
    X

    Given a movement command
    X
    , cut the appropriate number
    of characters, words, or lines from the current cursor
    position and go into insert mode.

    cc

    Cut the entire current line and
    go into insert mode.

    C

    Cut from the current cursor
    position to the end of the line and enter insert mode.
    (This is equivalent to
    c$
    .)

    y
    X

    Given a movement command
    X
    , copy (yank) the appropriate
    number of characters, words, or lines from the current
    cursor position.

    yy
    or
    Y

    Copy the entire current
    line.

    p

    Paste after the current cursor
    position.

    P

    Paste before the current cursor
    position.

    .

    Repeat the last
    command.

    u

    Undo the last
    command.

    /
    regex

    Search forward for
    regex
    .

    ?
    regex

    Search backward for
    regex
    .

    n

    Find the next
    match.

    N

    Find the previous match. (In
    other words, repeat the last search in the opposite
    direction.)

    :n

    Next file; when multiple files
    are specified for editing, this command loads the next
    file. Force this action (if the current file has unsaved
    changes) with
    :n!
    .

    :e
    file

    Load
    file
    in place of the current
    file. Force this action with
    :e!
    file
    .

    :r
    file

    Insert the contents of
    file
    after the current cursor
    position.

    :q

    Quit without saving changes.
    Force this action with
    :q!
    .

    :w
    file

    Write the current buffer to
    file
    . To append to an existing
    file, use
    :w
    >>
    file
    . Force
    the write (when possible, such as when running as root)
    with
    :w!
    file
    .

    :wq

    Write the file contents and
    quit. Force this action with
    :wq!
    .

    :x

    Write the file contents (if
    changed) and quit (the
    ex
    equivalent of
    ZZ
    ).

    ZZ

    Write the file contents (if
    changed) and quit.

    :!
    command

    Execute
    command
    in a
    subshell.

Other books

Please Don't Stop The Music by Lovering, Jane
THE PAIN OF OTHERS by Crouch, Blake
Croissants and Jam by Lynda Renham
No Place for Heroes by Laura Restrepo
Weapon of Blood by Chris A. Jackson
The Contemporary Buttercream Bible by Valeriano, Valeri, Ong, Christina
Dream House by Catherine Armsden
The Starwolves by Thorarinn Gunnarsson
Broken by Matthew Storm