42
Chapter 9 Managing Linux Processes

Linux+ Guide to Linux Certification, Third Edition

  • Upload
    maura

  • View
    96

  • Download
    0

Embed Size (px)

DESCRIPTION

Chapter 9 Managing Linux Processes. Linux+ Guide to Linux Certification, Third Edition. Objectives. Categorize the different types of processes on a Linux system View processes using standard Linux utilities Explain the difference between common kill signals - PowerPoint PPT Presentation

Citation preview

Page 1: Linux+ Guide to Linux Certification, Third Edition

Chapter 9Managing Linux Processes

Page 2: Linux+ Guide to Linux Certification, Third Edition

Objectives Categorize the different types of

processes on a Linux system View processes using standard Linux

utilities Explain the difference between common

kill signals Describe how binary programs and shell

scripts are executed

Linux+ Guide to Linux Certification, 3e 2

Page 3: Linux+ Guide to Linux Certification, Third Edition

Objectives (continued) Create and manipulate background

processes Use standard Linux utilities to modify the

priority of a process Schedule commands to execute in the

future using the at daemon Schedule commands to execute

repetitively using the cron daemon

Linux+ Guide to Linux Certification, 3e 3

Page 4: Linux+ Guide to Linux Certification, Third Edition

Linux Processes Program: structured set of commands

stored in an executable fileExecuted to create a process

Process: program running in memory and on CPU

User process: process begun by user on a terminal

Daemon process: system process Not associated with a terminal

Linux+ Guide to Linux Certification, 3e 4

Page 5: Linux+ Guide to Linux Certification, Third Edition

Linux Processes (continued) Process ID (PID): unique identifier

assigned to a process Child process: process started by another

process (parent process) Parent process: process that has started

other processes (child processes) Parent Process ID (PPID): PID of the

parent process The init daemon has a PID of 1 and a PPID

of 0

Linux+ Guide to Linux Certification, 3e 5

Page 6: Linux+ Guide to Linux Certification, Third Edition

Linux Processes (continued)

Linux+ Guide to Linux Certification, 3e 6

Figure 9-1: Parent and child processes

Page 7: Linux+ Guide to Linux Certification, Third Edition

Linux Processes (continued)

Linux+ Guide to Linux Certification, 3e 7

Figure 9-2: Process genealogy

Page 8: Linux+ Guide to Linux Certification, Third Edition

Viewing Processes ps command: view processes

Most versatile and common process viewing utility

No arguments: lists all processes running in current shell○ PID, terminal, command that started process, CPU

time–f (full) option: more complete information

○ User identifier (UID), PPID, start time, CPU utilization

-e option: displays the entire list of processes across all terminals including daemons

Linux+ Guide to Linux Certification, 3e 8

Page 9: Linux+ Guide to Linux Certification, Third Edition

Viewing Processes (continued) Process flag: indicates particular

features of the process Process state: current processor state of

processMost processes sleeping (S) or running (R)

Zombie process: process finished, but parent has not released child process’s PIDDefunct processProcess state is Z

Linux+ Guide to Linux Certification, 3e 9

Page 10: Linux+ Guide to Linux Certification, Third Edition

Viewing Processes (continued) Process priority (PRI): priority used by

the kernel for the processHigher value means lower priority

Nice value (NI): can be used to affect the process priority indirectlyHigher value means a greater chance of low

priority pstree command: displays the lineage

of a process by tracing its PPIDs until the init daemon

Linux+ Guide to Linux Certification, 3e 10

Page 11: Linux+ Guide to Linux Certification, Third Edition

Viewing Processes (continued)

Linux+ Guide to Linux Certification, 3e 11

Table 9-1: Common options to the ps command

Page 12: Linux+ Guide to Linux Certification, Third Edition

Viewing Processes (continued) top command: displays interactive screen

listing processesOrganized by processor timeProcesses using most processor time listed first

Rogue process: faulty process Consumes excessive system resources

top command can be used to change PRI or kill processesRogue processes can be killed immediately

when identified

Linux+ Guide to Linux Certification, 3e 12

Page 13: Linux+ Guide to Linux Certification, Third Edition

Killing Processes kill command: sends a kill signal to a

process to terminate it64 types of kill signals

○ Affect processes in different ways -l option: displays list of kill signal names

and associated numbers To kill a process, give kill signal and PID

○ If no kill signal given, the default kill signal, SIGTERM, is used

Linux+ Guide to Linux Certification, 3e 13

Page 14: Linux+ Guide to Linux Certification, Third Edition

Killing Processes (continued)

Linux+ Guide to Linux Certification, 3e 14

Table 9-2: Common administrative kill signals

Page 15: Linux+ Guide to Linux Certification, Third Edition

Killing Processes (continued) Trapping: ignoring a kill signal

The SIGKILL signal cannot be trapped by any process○ Use only as last resort

When parent process receives a kill signal, parent process terminates all child processes before terminating itselfTo kill several related processes send signal to

parent processKill parent process in order to kill zombie

processes

Linux+ Guide to Linux Certification, 3e 15

Page 16: Linux+ Guide to Linux Certification, Third Edition

Killing Processes (continued) killall command: kills multiple

processes of the same name in one commandTakes kill signal number as an optionUses process name instead of PIDIf no kill signal given, the default kill signal,

SIGTERM, is used Can use top command to kill processes

Linux+ Guide to Linux Certification, 3e 16

Page 17: Linux+ Guide to Linux Certification, Third Edition

Process Execution Three main types of executable

commandsBinary programs

○ e.g., ls, find, grepShell scriptsShell functions

○ e.g., cd, exit

Linux+ Guide to Linux Certification, 3e 17

Page 18: Linux+ Guide to Linux Certification, Third Edition

Process Execution (continued) Forking: act of creating new BASH shell

or subshellCarried out by fork function in BASH shellSubshell executes program or shell script

using exec functionOriginal shell waits for subshell to completeWhen done, subshell kills itself

○ Control returns to original shell

Linux+ Guide to Linux Certification, 3e 18

Page 19: Linux+ Guide to Linux Certification, Third Edition

Process Execution (continued)

Linux+ Guide to Linux Certification, 3e 19

Figure 9-3: Process forking

Page 20: Linux+ Guide to Linux Certification, Third Edition

Running Processes in the Background Foreground processes: BASH shell

must wait for termination to display prompt and accept new commands

Background processes: BASH shell does not wait for terminationAppend & metacharacter to commandUpon execution, user receives BASH shell

prompt immediately

Linux+ Guide to Linux Certification, 3e 20

Page 21: Linux+ Guide to Linux Certification, Third Edition

Running Processes in the Background (continued) jobs command: lists background job

IDs for processes running in current shell

To terminate background process:Send kill signal to PIDSend kill signal to background job ID

○ Prefix job ID with % character

Linux+ Guide to Linux Certification, 3e 21

Page 22: Linux+ Guide to Linux Certification, Third Edition

Running Processes in the Background (continued) foreground (fg) command: move a

background process to the foreground Use Ctrl+z to pause a foreground process

Assigns the process a background job ID background (bg) command: send an

existing process to the backgroundProvide background job ID as argument

jobs command indicates the two most recent background processesBy default, commands apply to most recent

process

Linux+ Guide to Linux Certification, 3e 22

Page 23: Linux+ Guide to Linux Certification, Third Edition

Process Priorities Time slice: amount of time a process is

given on a CPUMore time slices mean more execution time

on CPU○ Executes faster

Usually measured in milliseconds

Linux+ Guide to Linux Certification, 3e 23

Page 24: Linux+ Guide to Linux Certification, Third Edition

Process Priorities (continued) PRI dictates number of time slices a

process getsLow PRI is likely to get more time slices than

high PRICannot change PRI value directlySet NI to indirectly affect priority

○ Negative NI value, more time slices; positive NI value, less time slices

Processes start with NI of 0 nice command: change a process’s

priority as it starts

Linux+ Guide to Linux Certification, 3e 24

Page 25: Linux+ Guide to Linux Certification, Third Edition

Process Priorities (continued)

Linux+ Guide to Linux Certification, 3e 25

Figure 9-4: The nice value scale

Page 26: Linux+ Guide to Linux Certification, Third Edition

Process Priorities (continued) renice command: alter NI of a process

after it has been startedOnly root user may change NI to a negative

value-u option: change the NI for all processes

owned by the specified user or group May also change NI of running

processes using top utility

Linux+ Guide to Linux Certification, 3e 26

Page 27: Linux+ Guide to Linux Certification, Third Edition

Scheduling Commands To schedule commands to execute in

the future:at daemon (atd): system daemon that

executes tasks at a future timecron daemon (crond): system daemon that

executes tasks repetitively in the future

Linux+ Guide to Linux Certification, 3e 27

Page 28: Linux+ Guide to Linux Certification, Third Edition

Scheduling Commands with atd at command: schedule commands and

tasks to run at a preset timeSpecify the time as an argument–l option: view a list of at job IDs (regular

users see only their own jobs)atq command: alias to at -l

–c option: view content of a specified at job–d option: delete the specified at job–f option: list commands to be scheduled by at from a shell script

Linux+ Guide to Linux Certification, 3e 28

Page 29: Linux+ Guide to Linux Certification, Third Edition

Scheduling Commands with atd (continued)

Linux+ Guide to Linux Certification, 3e 29

Table 9-3: Common at commands

Page 30: Linux+ Guide to Linux Certification, Third Edition

Scheduling Commands with atd (continued) at daemon uses current shell’s

environment for executionShell environment and scheduled

commands stored in /var/spool/at If stdout of scheduled command has not

been redirected to a file, it is mailed to user

Linux+ Guide to Linux Certification, 3e 30

Page 31: Linux+ Guide to Linux Certification, Third Edition

Scheduling Commands with atd (continued) /etc/at.allow: file listing all users allowed

to use the at daemon /etc/at.deny: file listing all the users not

allowed to use the at daemon If both files exist, only /etc/at.allow file is

processed On Fedora Linux systems, only

/etc/at.deny file exists by defaultInitially left blank, all users allowed to use at

daemon

Linux+ Guide to Linux Certification, 3e 31

Page 32: Linux+ Guide to Linux Certification, Third Edition

Scheduling Commands with crond Suitable for scheduling repetitive tasks Cron tables: configuration files

specifying when commands should be executedSix fields separated by space or tab

characters○ First five specify times to run the command○ Sixth absolute pathname to command to be

executed

Linux+ Guide to Linux Certification, 3e 32

Page 33: Linux+ Guide to Linux Certification, Third Edition

Scheduling Commands with crond (continued) User cron tables: represent tasks

scheduled by individual users System cron tables: contains system

tasks /var/spool/cron: stores user cron tables /etc/crontab file: contains system cron

tables /etc/cron.d directory: contains system

cron tables

Linux+ Guide to Linux Certification, 3e 33

Page 34: Linux+ Guide to Linux Certification, Third Edition

Scheduling Commands with crond (continued)

Linux+ Guide to Linux Certification, 3e 34

Figure 9-5: User cron table format

Page 35: Linux+ Guide to Linux Certification, Third Edition

Scheduling Commands with crond (continued)

Linux+ Guide to Linux Certification, 3e 35

Figure 9-6: Sample user cron table entry

Page 36: Linux+ Guide to Linux Certification, Third Edition

User Cron Tables /etc/cron.allow: lists users allowed to

use the cron daemon /etc/cron.deny: lists users not allowed to

use the cron daemon If both files exist, only /etc/cron.allow file

is processed On Fedora Linux systems, only

/etc/cron.deny file exists by defaultInitially left blank, all users allowed to use

cron daemon

Linux+ Guide to Linux Certification, 3e 36

Page 37: Linux+ Guide to Linux Certification, Third Edition

User Cron Tables (continued) crontab command: view and edit user

cron tables–e option: edit cron tables in vi editor–l option: list a user cron table–r option: remove cron table and all

scheduled jobs-u option: used by root user to edit, list, or

remove a specified user’s cron table

Linux+ Guide to Linux Certification, 3e 37

Page 38: Linux+ Guide to Linux Certification, Third Edition

The System Cron Table System maintenance, backups, and

CPU-intensive tasks often scheduled for non-business hoursOften scheduled by cron daemon

○ Entries in system cron table (/etc/crontab)

Linux+ Guide to Linux Certification, 3e 38

Page 39: Linux+ Guide to Linux Certification, Third Edition

The System Cron Table (continued) Initial section of cron table specifies

execution environment Remainder similar to user cron table

entries Cron tables located in the /etc/cron.d

directory are run by the system as a specified user

Can schedule administrative task by placing a shell script in the appropriate one of the following directories: /etc/cron.hourly/, /etc/cron.daily/, /etc/cron.weekly, and etc/cron.monthly/

Linux+ Guide to Linux Certification, 3e 39

Page 40: Linux+ Guide to Linux Certification, Third Edition

Summary Processes are programs that are

executing on the system User processes are run in the same

terminal as the user who executed them, whereas daemon processes are system processes that do not run on a terminal

Every process has a parent process associated with it and, optionally, several child processes

Linux+ Guide to Linux Certification, 3e 40

Page 41: Linux+ Guide to Linux Certification, Third Edition

Summary (continued) Process information is stored in the /proc

filesystem; the ps, pstree and top commands can be used to view this information

Zombie and rogue processes that exist for long periods of time use up system resources and should be killed to improve system performance

You can send kill signals to a process using the kill, killall, and top commands

Linux+ Guide to Linux Certification, 3e 41

Page 42: Linux+ Guide to Linux Certification, Third Edition

Summary (continued) The BASH shell creates, or forks, a

subshell to execute most commands Processes can be run in the background

by appending an & to the command name

The priority of a process can be affected indirectly by altering its nice value

Commands can be scheduled to run at a later time using the at and cron daemons

Linux+ Guide to Linux Certification, 3e 42