Transcript
Page 1: Linux+ Guide to Linux Certification, Third Edition Chapter 9 Managing Linux Processes

Linux+ Guide to Linux Certification, Third Edition

Chapter 9Managing Linux Processes

Page 2: Linux+ Guide to Linux Certification, Third Edition Chapter 9 Managing Linux Processes

Linux+ Guide to Linux Certification, 3e 2

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

Page 3: Linux+ Guide to Linux Certification, Third Edition Chapter 9 Managing Linux Processes

Linux+ Guide to Linux Certification, 3e 3

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

Page 4: Linux+ Guide to Linux Certification, Third Edition Chapter 9 Managing Linux Processes

Linux+ Guide to Linux Certification, 3e 4

Linux Processes

• Program: structured set of commands stored in an executable file– Executed 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

Page 5: Linux+ Guide to Linux Certification, Third Edition Chapter 9 Managing Linux Processes

Linux+ Guide to Linux Certification, 3e 5

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

Page 6: Linux+ Guide to Linux Certification, Third Edition Chapter 9 Managing Linux Processes

Linux+ Guide to Linux Certification, 3e 6

Linux Processes (continued)

Figure 9-1: Parent and child processes

Page 7: Linux+ Guide to Linux Certification, Third Edition Chapter 9 Managing Linux Processes

Linux+ Guide to Linux Certification, 3e 7

Linux Processes (continued)

Figure 9-2: Process genealogy

Page 8: Linux+ Guide to Linux Certification, Third Edition Chapter 9 Managing Linux Processes

Linux+ Guide to Linux Certification, 3e 8

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

Page 9: Linux+ Guide to Linux Certification, Third Edition Chapter 9 Managing Linux Processes

Viewing Processes (continued)

• Process flag: indicates particular features of the process

• Process state: current processor state of process– Most processes sleeping (S) or running (R)

• Zombie process: process finished, but parent has not released child process’s PID– Defunct process– Process state is Z

Linux+ Guide to Linux Certification, 3e 9

Page 10: Linux+ Guide to Linux Certification, Third Edition Chapter 9 Managing Linux Processes

Linux+ Guide to Linux Certification, 3e 10

Viewing Processes (continued)

• Process priority (PRI): priority used by the kernel for the process– Higher value means lower priority

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

• pstree command: displays the lineage of a process by tracing its PPIDs until the init daemon

Page 11: Linux+ Guide to Linux Certification, Third Edition Chapter 9 Managing Linux Processes

Linux+ Guide to Linux Certification, 3e 11

Viewing Processes (continued)

Table 9-1: Common options to the ps command

Page 12: Linux+ Guide to Linux Certification, Third Edition Chapter 9 Managing Linux Processes

Linux+ Guide to Linux Certification, 3e 12

Viewing Processes (continued)

• top command: displays interactive screen listing processes– Organized by processor time– Processes using most processor time listed first

• Rogue process: faulty process – Consumes excessive system resources

• top command can be used to change PRI or kill processes– Rogue processes can be killed immediately when

identified

Page 13: Linux+ Guide to Linux Certification, Third Edition Chapter 9 Managing Linux Processes

Linux+ Guide to Linux Certification, 3e 13

Killing Processes

• kill command: sends a kill signal to a process to terminate it– 64 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

Page 14: Linux+ Guide to Linux Certification, Third Edition Chapter 9 Managing Linux Processes

Linux+ Guide to Linux Certification, 3e 14

Killing Processes (continued)

Table 9-2: Common administrative kill signals

Page 15: Linux+ Guide to Linux Certification, Third Edition Chapter 9 Managing Linux Processes

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 itself– To kill several related processes send signal to

parent process– Kill parent process in order to kill zombie processes

Linux+ Guide to Linux Certification, 3e 15

Page 16: Linux+ Guide to Linux Certification, Third Edition Chapter 9 Managing Linux Processes

Linux+ Guide to Linux Certification, 3e 16

Killing Processes (continued)

• killall command: kills multiple processes of the same name in one command– Takes kill signal number as an option– Uses process name instead of PID– If no kill signal given, the default kill signal,

SIGTERM, is used

• Can use top command to kill processes

Page 17: Linux+ Guide to Linux Certification, Third Edition Chapter 9 Managing Linux Processes

Linux+ Guide to Linux Certification, 3e 17

Process Execution

• Three main types of executable commands– Binary programs

• e.g., ls, find, grep

– Shell scripts– Shell functions

• e.g., cd, exit

Page 18: Linux+ Guide to Linux Certification, Third Edition Chapter 9 Managing Linux Processes

Linux+ Guide to Linux Certification, 3e 18

Process Execution (continued)

• Forking: act of creating new BASH shell or subshell– Carried out by fork function in BASH shell– Subshell executes program or shell script using exec

function– Original shell waits for subshell to complete– When done, subshell kills itself

• Control returns to original shell

Page 19: Linux+ Guide to Linux Certification, Third Edition Chapter 9 Managing Linux Processes

Linux+ Guide to Linux Certification, 3e 19

Process Execution (continued)

Figure 9-3: Process forking

Page 20: Linux+ Guide to Linux Certification, Third Edition Chapter 9 Managing Linux Processes

Linux+ Guide to Linux Certification, 3e 20

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 termination– Append & metacharacter to command– Upon execution, user receives BASH shell prompt

immediately

Page 21: Linux+ Guide to Linux Certification, Third Edition Chapter 9 Managing Linux Processes

Linux+ Guide to Linux Certification, 3e 21

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 PID– Send kill signal to background job ID

• Prefix job ID with % character

Page 22: Linux+ Guide to Linux Certification, Third Edition Chapter 9 Managing Linux Processes

Linux+ Guide to Linux Certification, 3e 22

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 background– Provide background job ID as argument

• jobs command indicates the two most recent background processes– By default, commands apply to most recent process

Page 23: Linux+ Guide to Linux Certification, Third Edition Chapter 9 Managing Linux Processes

Linux+ Guide to Linux Certification, 3e 23

Process Priorities

• Time slice: amount of time a process is given on a CPU– More time slices mean more execution time on CPU

• Executes faster

– Usually measured in milliseconds

Page 24: Linux+ Guide to Linux Certification, Third Edition Chapter 9 Managing Linux Processes

Linux+ Guide to Linux Certification, 3e 24

Process Priorities (continued)

• PRI dictates number of time slices a process gets– Low PRI is likely to get more time slices than high

PRI– Cannot change PRI value directly– Set 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

Page 25: Linux+ Guide to Linux Certification, Third Edition Chapter 9 Managing Linux Processes

Linux+ Guide to Linux Certification, 3e 25

Process Priorities (continued)

Figure 9-4: The nice value scale

Page 26: Linux+ Guide to Linux Certification, Third Edition Chapter 9 Managing Linux Processes

Linux+ Guide to Linux Certification, 3e 26

Process Priorities (continued)

• renice command: alter NI of a process after it has been started– Only 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

Page 27: Linux+ Guide to Linux Certification, Third Edition Chapter 9 Managing Linux Processes

Linux+ Guide to Linux Certification, 3e 27

Scheduling Commands

• To schedule commands to execute in the future:– at daemon (atd): system daemon that executes

tasks at a future time– cron daemon (crond): system daemon that executes

tasks repetitively in the future

Page 28: Linux+ Guide to Linux Certification, Third Edition Chapter 9 Managing Linux Processes

Linux+ Guide to Linux Certification, 3e 28

Scheduling Commands with atd

• at command: schedule commands and tasks to run at a preset time– Specify 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

Page 29: Linux+ Guide to Linux Certification, Third Edition Chapter 9 Managing Linux Processes

Linux+ Guide to Linux Certification, 3e 29

Scheduling Commands with atd (continued)

Table 9-3: Common at commands

Page 30: Linux+ Guide to Linux Certification, Third Edition Chapter 9 Managing Linux Processes

Linux+ Guide to Linux Certification, 3e 30

Scheduling Commands with atd (continued)

• at daemon uses current shell’s environment for execution– Shell 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

Page 31: Linux+ Guide to Linux Certification, Third Edition Chapter 9 Managing Linux Processes

Linux+ Guide to Linux Certification, 3e 31

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 default– Initially left blank, all users allowed to use at daemon

Page 32: Linux+ Guide to Linux Certification, Third Edition Chapter 9 Managing Linux Processes

Scheduling Commands with crond

• Suitable for scheduling repetitive tasks

• Cron tables: configuration files specifying when commands should be executed– Six 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 Chapter 9 Managing Linux Processes

Linux+ Guide to Linux Certification, 3e 33

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

Page 34: Linux+ Guide to Linux Certification, Third Edition Chapter 9 Managing Linux Processes

Linux+ Guide to Linux Certification, 3e 34

Scheduling Commands with crond (continued)

Figure 9-5: User cron table format

Page 35: Linux+ Guide to Linux Certification, Third Edition Chapter 9 Managing Linux Processes

Linux+ Guide to Linux Certification, 3e 35

Scheduling Commands with crond (continued)

Figure 9-6: Sample user cron table entry

Page 36: Linux+ Guide to Linux Certification, Third Edition Chapter 9 Managing Linux Processes

Linux+ Guide to Linux Certification, 3e 36

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 default– Initially left blank, all users allowed to use cron

daemon

Page 37: Linux+ Guide to Linux Certification, Third Edition Chapter 9 Managing Linux Processes

Linux+ Guide to Linux Certification, 3e 37

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

Page 38: Linux+ Guide to Linux Certification, Third Edition Chapter 9 Managing Linux Processes

Linux+ Guide to Linux Certification, 3e 38

The System Cron Table

• System maintenance, backups, and CPU-intensive tasks often scheduled for non-business hours– Often scheduled by cron daemon

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

Page 39: Linux+ Guide to Linux Certification, Third Edition Chapter 9 Managing Linux Processes

Linux+ Guide to Linux Certification, 3e 39

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/

Page 40: Linux+ Guide to Linux Certification, Third Edition Chapter 9 Managing Linux Processes

Linux+ Guide to Linux Certification, 3e 40

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

Page 41: Linux+ Guide to Linux Certification, Third Edition Chapter 9 Managing Linux Processes

Linux+ Guide to Linux Certification, 3e 41

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

Page 42: Linux+ Guide to Linux Certification, Third Edition Chapter 9 Managing Linux Processes

Linux+ Guide to Linux Certification, 3e 42

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


Recommended