+ All Categories
Home > Technology > Unit 10 investigating and managing

Unit 10 investigating and managing

Date post: 26-Jun-2015
Category:
Upload: rootfibo
View: 1,104 times
Download: 2 times
Share this document with a friend
Popular Tags:
26
RedHat Enterprise Linux Essential Unit 10: Investigating and Managing Processes
Transcript
Page 1: Unit 10 investigating and managing

RedHat Enterprise Linux Essential

Unit 10: Investigating and Managing

Processes

Page 2: Unit 10 investigating and managing

Investigating and Managing Processes

Upon completion of this unit, you should be able to:

Explain what a process is

Describe how to manage processes

Use job control tools

Page 3: Unit 10 investigating and managing

What is a Process?

A process is a set of instructions loaded into memory

Numeric Process ID (PID) used for identification

UID, GID and SELinux context determines filesystem access

• Normally inherited from the executing user

Page 4: Unit 10 investigating and managing

Listing Processes

View Process information with ps

Shows processes from the current terminal by default

a includes processes on all terminals

x includes processes not attached to terminals

u prints process owner information

f prints process parentage

o PROPERTY,... prints custom information:

• pid, comm, %cpu, %mem, state, tty, euser, ruser

Process states – running, sleeping, uninterruptable sleep, zombie.

Page 5: Unit 10 investigating and managing

Finding Processes

Most flexible: ps options | other commands

ps axo comm,tty | grep ttyS0

ps -ef

By predefined patterns: pgrep

$ pgrep -U root

$ pgrep -G student

● By exact program name: pidof

$ pidof bash

Page 6: Unit 10 investigating and managing

Example with cpu

ps –Ao pcpu,pid,user,args | sort –k 1 –r |head -10

Page 7: Unit 10 investigating and managing

Signals

Most fundamental inter-process communication

Sent directly to processes, no user-interface required

Programs associate actions with each signal

Signals are specified by name or number when sent:

• Signal 15, TERM (default) - Terminate cleanly (stopping)

• Signal 9, KILL - Terminate immediately (kill)

• Signal 1, HUP - Re-read configuration files (reload)

• Kill –l list info for singal

• man 7 signal shows complete list

Page 8: Unit 10 investigating and managing

Sending Signals to Processes

By PID: kill [signal] pid ...

By Name: killall [signal] comm ...

By pattern: pkill [-signal] pattern

Page 9: Unit 10 investigating and managing

Scheduling Priority

Scheduling priority determines access to the CPU

Priority is affected by a process‘ nice value

Values range from -20 to 19 but default to 0

Lower nice value means higher CPU priority

Viewed with ps -Ao comm,nice

ex: firefox &

ps –Ao comm,nice |grep firefox

Page 10: Unit 10 investigating and managing

Altering Scheduling Priority

Nice values may be altered...

When starting a process:

$ nice -n 5 command

After starting:

$ renice 5 PID

renice 5 –p PID

Only root may decrease nice values

Page 11: Unit 10 investigating and managing

Interactive Process Management Tools

CLI: top

GUI: gnome-system-monitor

Capabilities

Display real-time process information

Allow sorting, killing and re-nicing

Page 12: Unit 10 investigating and managing

Job Control

Run a process in the background

Append an ampersand to the command line: firefox &

Temporarily halt a running program

Use Ctrl-z or send signal 17 (STOP)

Manage background or suspended jobs

List job numbers and names: jobs

Resume in the background: bg [%jobnum]

Resume in the foreground: fg [%jobnum]

Send a signal: kill [-SIGNAL] [%jobnum]

Page 13: Unit 10 investigating and managing

Scheduling a Process To Execute Later

One-time jobs use at, recurring jobs use crontab

Non-redirected output is mailed to the user

root can modify jobs for other users

Create

List

Details

Remove

Edit

at time crontab -e

at -l crontab -l

at -c jobnum N/A

at -d jobnum crontab -r

N/A crontab -e

Page 14: Unit 10 investigating and managing

Using at

Setting an at job: at [options] time

Options:

-b run command only when the system load is low

-d job# delete an at job from queue

-f filename read job from a specified file

-l list jobs for that user (all jobs for root)

-m mail user quen job completes

-q queuename send the jobs to a queue (a to z and A to Z)

Time formats:

now, 17:00, +3 hours, +2 minutes, +2 days, +3 months, 19:15 3.12.10,

midnight, 4PM, 16:00 +3 days, mon, tomorrow …

Show the at jobs queue of user: atq or at –l

Deletes at jobs from the jobs queue: atrm job# [job#] …

Page 15: Unit 10 investigating and managing

Examples with at

Create an simple at job to run in 5 minutes later

$ at now+5 minutes

echo “I am running in an at job” > /tmp/test_cron

[Ctrl-D]

Create an at job which read commands from a file and run at

midnight

$ at –f /tmp/myjob.sh midnight

Using echo to run multiple commands with at

$ echo „cd /tmp; ls –a > /tmp/test_cron ‟ | at now+2 minutes

Listing all at jobs

$ at –l

$ atq

Page 16: Unit 10 investigating and managing

crontab commands

Create/edit a user crontab: crontab –e

Create a user crontab by reading from file:

crontab –e filename

Display user‟s crontab file: crontab –l

Delete user‟s crontab file: crontab –r

Edit a user‟s crontab file (for root only):

crontab –e –u username

Page 17: Unit 10 investigating and managing

Crontab File Format

Entry consists of five space-delimited fields followed by a

command line

01 * * * * root cd /tmp && ls -laht

One entry per line, no limit to line length

Fields are minute, hour, day of month, month, and day of week

Comment lines begin with #

See man 5 crontab for details

Page 18: Unit 10 investigating and managing

Examples of user crontabs

Run a command every hour from 8:00 to 18:00 everyday

0 8-18 * * * <command>

Run a command every 4 hours on the half hour (i.e 6:30, 10:30, 14:30, 16:30)

everyday

30 6-16/4 * * * <command>

Run a command every day, Monday to Friday at 01:00, and doesn‟t report to

syslog

-0 1 * * 1-5 <command>

Run the command every Monday and Tuesday at 12:00, 12:10, 12:20, 12:30

0,10,20,30 12 * * 1,2 <command>

Run a command every 10 minutes

*/10 * * * * <command>

echo “00 21 * * 7 root rm -f /tmp/*.log” >> /etc/crontab

crontab -e

00 8-17 * * 1-5 du -sh $HOME >> /tmp/diskreport

Page 19: Unit 10 investigating and managing

Grouping Commands

Two ways to group commands:

Compound: date; who | wc -l

• Commands run back-to-back

Subshell: (date; who | wc -l) >> /tmp/trace

• All output is sent to a single STDOUT and STDERR

Page 20: Unit 10 investigating and managing

Exit Status

Processes report success or failure with an exit status

0 for success, 1-255 for failure

$? stores the exit status of the most recent command

exit [num] terminates and sets status to num

Example:

$ ping -c1 -W1 localhost999 &> /dev/null

$ echo $?

2

Page 21: Unit 10 investigating and managing

Conditional Execution Operators

Commands can be run conditionally based on exit status

&& represents conditional AND THEN

|| represents conditional OR ELSE

Examples:

$ grep -q no_such_user /etc/passwd || echo 'No such user'

No such user

$ ping localhost &> /dev/null \

> && echo “localhost is up" \

> || echo ‘localhost is unreachable’

localhost is up

Page 22: Unit 10 investigating and managing

The test Command

Evaluates boolean statements for use in conditional execution

Returns 0 for true

Returns 1 for false

Examples in long form:

test "$A" = "$B" && echo "string" || echo "not equal"

$ test "$A" -eq "$B" && echo "Integers are equal“

Examples in shorthand notation:

$ [ "$A" = "$B" ] && echo "Strings are equal"

$ [ "$A" -eq "$B" ] && echo "Integers are equal"

Page 23: Unit 10 investigating and managing

File Tests

File tests:

-f tests to see if a file exists and is a regular file

-d tests to see if a file exists and is a directory

-x tests to see if a file exists and is executable

[ -f ~/lib/functions ] && source ~/lib/functions

Page 24: Unit 10 investigating and managing

File Tests (cont’)

Options Mean

-d file True if the file is a directory.

-e file True if the file exists.

-f file True if the file exists and is a regular file.

-h file True if the file is a symbolic link.

-L file True if the file is a symbolic link.

-r file True if the file exists and is readable by you.

-s file True if the file exists and is not empty.

-w file True if the file exists and is writable by you.

-x file True if the file exists and is executable by you.

-O file True if the file is effectively owned by you.

-G file True if the file is effectively owned by your group.

Page 25: Unit 10 investigating and managing

Scripting: if Statements

Execute instructions based on the exit status of a command

if ping -c1 -w2 station1 &> /dev/null; then

echo 'Station1 is UP'

elif grep "station1" ~/maintenance.txt &> /dev/null; then

echo 'Station1 is undergoing maintenance'

else

echo 'Station1 is unexpectedly DOWN!'

exit 1

fi

Page 26: Unit 10 investigating and managing

Recommended