linux+ guide to linux certification, second edition chapter 10 managing linux processes

42
Linux+ Guide to Linux Certification, Second Edition Chapter 10 Managing Linux Processes

Upload: caroline-holt

Post on 04-Jan-2016

236 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Linux+ Guide to Linux Certification, Second Edition Chapter 10 Managing Linux Processes

Linux+ Guide to Linux Certification, Second Edition

Chapter 10Managing Linux Processes

Page 2: Linux+ Guide to Linux Certification, Second Edition Chapter 10 Managing Linux Processes

Linux+ Guide to Linux Certification, 2e 2

Objectives

• Categorize the different types of processes on a Linux system

• View processes using standard Linux utilities

• Illustrate the difference between common kill signals

• Describe how binary programs and shell scripts are executed

Page 3: Linux+ Guide to Linux Certification, Second Edition Chapter 10 Managing Linux Processes

Linux+ Guide to Linux Certification, 2e 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, Second Edition Chapter 10 Managing Linux Processes

Linux+ Guide to Linux Certification, 2e 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, Second Edition Chapter 10 Managing Linux Processes

Linux+ Guide to Linux Certification, 2e 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): Parent process’s PID

Page 6: Linux+ Guide to Linux Certification, Second Edition Chapter 10 Managing Linux Processes

Linux+ Guide to Linux Certification, 2e 6

Linux Processes (continued)

Figure 10-1: Parent and child processes

Page 7: Linux+ Guide to Linux Certification, Second Edition Chapter 10 Managing Linux Processes

Linux+ Guide to Linux Certification, 2e 7

Linux Processes (continued)

Figure 10-2: Process genealogy

Page 8: Linux+ Guide to Linux Certification, Second Edition Chapter 10 Managing Linux Processes

Linux+ Guide to Linux Certification, 2e 8

Viewing Processes

• ps command: View processes– Most 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

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

Linux+ Guide to Linux Certification, 2e 9

Viewing Processes (continued)

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

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

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

Linux+ Guide to Linux Certification, 2e 10

Viewing Processes (continued)

• Process priority (PRI): Determines how many processor time slices process will receive– Higher value means lower priority

• Nice value (NI): Indirectly represents priority– Higher value means lower priority

Page 11: Linux+ Guide to Linux Certification, Second Edition Chapter 10 Managing Linux Processes

Linux+ Guide to Linux Certification, 2e 11

Viewing Processes (continued)

Table 10-1: Common options to the ps command

Page 12: Linux+ Guide to Linux Certification, Second Edition Chapter 10 Managing Linux Processes

Linux+ Guide to Linux Certification, 2e 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

Page 13: Linux+ Guide to Linux Certification, Second Edition Chapter 10 Managing Linux Processes

Linux+ Guide to Linux Certification, 2e 13

Killing Processes

• kill signal: Sent to a process by kill command– 64 types of kill signals

• Affect processes in different ways

• kill command: Kills all instances of a process by command name– To kill a process, give kill signal and PID

• If no kill signal given, SIGTERM assumed

Page 14: Linux+ Guide to Linux Certification, Second Edition Chapter 10 Managing Linux Processes

Linux+ Guide to Linux Certification, 2e 14

Killing Processes (continued)

Table 10-2: Common administrative kill signals

Page 15: Linux+ Guide to Linux Certification, Second Edition Chapter 10 Managing Linux Processes

Linux+ Guide to Linux Certification, 2e 15

Killing Processes (continued)

• Trapping: Ignore a kill signal

• Kill signals sent to processes having children– Parent process terminates all child processes before

terminating itself

• Often necessary to kill parent process in order to kill zombie processes

Page 16: Linux+ Guide to Linux Certification, Second Edition Chapter 10 Managing Linux Processes

Linux+ Guide to Linux Certification, 2e 16

Killing Processes (continued)

• killall command: Kills all instances of a process by command name– Uses process name instead of PID

• Can use top command to kill processes

Page 17: Linux+ Guide to Linux Certification, Second Edition Chapter 10 Managing Linux Processes

Linux+ Guide to Linux Certification, 2e 17

Process Execution

• Three main types of executable commands– Binary programs– Shell scripts– Shell functions

Page 18: Linux+ Guide to Linux Certification, Second Edition Chapter 10 Managing Linux Processes

Linux+ Guide to Linux Certification, 2e 18

Process Execution (continued)

• Forking: Act of creating new BASH shell child process from a parent– 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, Second Edition Chapter 10 Managing Linux Processes

Linux+ Guide to Linux Certification, 2e 19

Process Execution (continued)

Figure 10-3: Process forking

Page 20: Linux+ Guide to Linux Certification, Second Edition Chapter 10 Managing Linux Processes

Linux+ Guide to Linux Certification, 2e 20

Running Processes in the Background

• Foreground processes: BASH shell must wait for termination

• Background processes: BASH shell does not wait for termination– & metacharacter– Upon execution, user receives BASH shell prompt

immediately

Page 21: Linux+ Guide to Linux Certification, Second Edition Chapter 10 Managing Linux Processes

Linux+ Guide to Linux Certification, 2e 21

Running Processes in the Background (continued)

• jobs command: Lists background processes running in current shell

• foreground (fg) command: Run a background process in foreground

• Foreground process may be paused with ctrl Z

• background (bg) command: Run a foreground process in background

Page 22: Linux+ Guide to Linux Certification, Second Edition Chapter 10 Managing Linux Processes

Linux+ Guide to Linux Certification, 2e 22

Running Processes in the Background (continued)

• Jobs command marks two most recent background processes– Commands operate on most recent process, by

default

Page 23: Linux+ Guide to Linux Certification, Second Edition Chapter 10 Managing Linux Processes

Linux+ Guide to Linux Certification, 2e 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, Second Edition Chapter 10 Managing Linux Processes

Linux+ Guide to Linux Certification, 2e 24

Process Priorities (continued)

• PRI dictates number of time slices a process gets– Cannot change PRI value directly– Set NI to indirectly affect priority

• Processes start with NI of 0

• nice command: Change a process’s priority as it starts

Page 25: Linux+ Guide to Linux Certification, Second Edition Chapter 10 Managing Linux Processes

Linux+ Guide to Linux Certification, 2e 25

Process Priorities (continued)

Figure 10-4: The nice value scale

Page 26: Linux+ Guide to Linux Certification, Second Edition Chapter 10 Managing Linux Processes

Linux+ Guide to Linux Certification, 2e 26

Process Priorities (continued)

• renice command: Alter NI of a process after it is running– Only root user may change NI to a negative value

• May also change NI of running processes using top utility

Page 27: Linux+ Guide to Linux Certification, Second Edition Chapter 10 Managing Linux Processes

Linux+ Guide to Linux Certification, 2e 27

Scheduling Commands

• 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, Second Edition Chapter 10 Managing Linux Processes

Linux+ Guide to Linux Certification, 2e 28

Scheduling Commands with atd

• at command: Schedule commands and tasks to run at a preset time– –l option: View a list of scheduled jobs

• Regular users see only their own jobs– –c option: View system environment at scheduling

time– –d option: Delete a job– –f option: Run scheduled jobs from shell script

Page 29: Linux+ Guide to Linux Certification, Second Edition Chapter 10 Managing Linux Processes

Linux+ Guide to Linux Certification, 2e 29

Scheduling Commands with atd (continued)

• atq command: Alternative method to view scheduled jobs

• 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 file, mailed to user

Page 30: Linux+ Guide to Linux Certification, Second Edition Chapter 10 Managing Linux Processes

Linux+ Guide to Linux Certification, 2e 30

Scheduling Commands with atd (continued)

Table 10-3: Common at commands

Page 31: Linux+ Guide to Linux Certification, Second Edition Chapter 10 Managing Linux Processes

Linux+ Guide to Linux Certification, 2e 31

Scheduling Commands with atd (continued)

• /etc/at.allow: File listing all users able to use at command

• /etc/at.deny: File listing all the users not able to use at command

• On Fedora Linux systems, no /etc/at.allow file exists– Empty by default

• All users allowed to use at daemon

Page 32: Linux+ Guide to Linux Certification, Second Edition Chapter 10 Managing Linux Processes

Linux+ Guide to Linux Certification, 2e 32

Scheduling Commands with crond

• Cron tables: Files specifying tasks for cron daemon to run – Specify when commands should be executed– User and system cron tables– Six fields separated by space or tab characters

Page 33: Linux+ Guide to Linux Certification, Second Edition Chapter 10 Managing Linux Processes

Linux+ Guide to Linux Certification, 2e 33

Scheduling Commands with crond (continued)

Figure 10-5: User cron table format

Page 34: Linux+ Guide to Linux Certification, Second Edition Chapter 10 Managing Linux Processes

Linux+ Guide to Linux Certification, 2e 34

Scheduling Commands with crond (continued)

Figure 10-6: Sample user cron table entry

Page 35: Linux+ Guide to Linux Certification, Second Edition Chapter 10 Managing Linux Processes

Linux+ Guide to Linux Certification, 2e 35

Scheduling Commands with crond (continued)

• /var/spool/cron: Stores user cron tables

• /etc/crontab: Contains default system cron table

• /etc/cron.d: Contains additional system cron tables

Page 36: Linux+ Guide to Linux Certification, Second Edition Chapter 10 Managing Linux Processes

Linux+ Guide to Linux Certification, 2e 36

User Cron Tables

• /etc/cron.allow: Lists users able to use cron command

• /etc/cron.deny: Lists users not able to access cron command

Page 37: Linux+ Guide to Linux Certification, Second Edition Chapter 10 Managing Linux Processes

Linux+ Guide to Linux Certification, 2e 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 all scheduled jobs

Page 38: Linux+ Guide to Linux Certification, Second Edition Chapter 10 Managing Linux Processes

Linux+ Guide to Linux Certification, 2e 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, Second Edition Chapter 10 Managing Linux Processes

Linux+ Guide to Linux Certification, 2e 39

The System Cron Table (continued)

• Initial section of cron table specifies execution environment

• Remainder similar to user cron table entries

• Sixth field specifies who to run command as

• Remaining fields represent command to run

• run-parts command: Execute all files in a directory

Page 40: Linux+ Guide to Linux Certification, Second Edition Chapter 10 Managing Linux Processes

Linux+ Guide to Linux Certification, 2e 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, Second Edition Chapter 10 Managing Linux Processes

Linux+ Guide to Linux Certification, 2e 41

Summary (continued)

• Process information is stored in the /proc filesystem; the ps 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

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

Page 42: Linux+ Guide to Linux Certification, Second Edition Chapter 10 Managing Linux Processes

Linux+ Guide to Linux Certification, 2e 42

Summary (continued)

• The BASH shell 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 NI

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