Workbook 9 Managing Processes RH030 Linux Computing Essentials.

Post on 04-Jan-2016

234 views 4 download

Tags:

Transcript of Workbook 9 Managing Processes RH030 Linux Computing Essentials.

Workbook 9 Managing Processes

RH030 Linux Computing Essentials

Linux+ Guide to Linux Certification, 2e

2

Objectives Today is all about understanding processes on a Linux system

Viewing processes local processes system processes

Terminating Processes Kill Commands Using kill signals

Modifying Processes Running background processes Modify the priority of a process

Schedule commands: at crondtab

Linux+ Guide to Linux Certification, 2e

3

Chapter 1 A process is an instance of a running executable, identified by a

process id (pid). It maintains a set of imformation relevant to the processes Such as virtual memory addresses, every process possesses its own distinct

memory context. A process has a uid and a collection of gid as credentials. A process has a filesystem context, including a cwd, a umask, a root directory,

and a collection of open files. A process has a scheduling context, including a niceness value. A process has a collection of environment variables.

The ps command can be used to examine all currently running processes.

The top command can be used to monitor all running processes.

Linux+ Guide to Linux Certification, 2e

4

Linux Processes Linux manages tasks using processes

Processes are programs that are executing on the system

A program is really just a structured set of commands stored in an executable file which runs in memory and on CPU

A Process can spawn a subprocess, thus creating a process hierarchy with parent / child relationships

Some simple commands, such as cd, are executed by the shell itself and so do not create a separate process

Each program creates a process which is assigned a unique process identification number (PID)

Linux+ Guide to Linux Certification, 2e

5

Kernel Allocation for Processes Processes are run by the CPU

Linux+ Guide to Linux Certification, 2e

6

All processes are generated. User process:

Task is begun by a local user Task is run as a process owned by the user from the local terminal This process is now associated with that specific user. User processes are run from the same terminal as the user who executed them. “ tty? “ or “ pty? “- from ps output identifies it as a user process

System process: System processes have been started by the kernel Or they are a service which has been turned on. They are not associated with a terminal Services are run by a Daemon process Which commonly run a network service “ ? “ - from the ps output identifies it as a system process

Linux+ Guide to Linux Certification, 2e

7

Viewing Processes ps command: Most commonly used command to view processes.

No arguments: List local processes only Brief listing only PID, TTY, CPU time, command that started process,

– f (full) option: Full detailed listing User identifier (UID), PID, PPID, TTY, start time, CPU utilization, command

– u option: See the processes that another user is using.

Linux+ Guide to Linux Certification, 2e

8

Overview of the ps Command$ ps [-options]

Linux+ Guide to Linux Certification, 2e

9

ps –f gives full output details See examples page 436 A full listing shows both PID and PPID

Linux+ Guide to Linux Certification, 2e

10

Viewing All Processes Running on

the System ps – ef command:

-e Lists all processes running on the system Brief listing only PID, TTY, CPU time, command that started process,

–f (full) option: Full detailed listing User identifier (UID), PID, PPID, start time, TTY, CPU utilization, command

Or you can use

ps – aux command:

Linux+ Guide to Linux Certification, 2e

11

SystemV verse BSD options page 441

Table 10-1: Common options to the ps command

Linux+ Guide to Linux Certification, 2e

12

ps –ef Command Output

Linux+ Guide to Linux Certification, 2e

13

pgrep

[maxwell@station maxwell]$ pgrep -l sshd

829 sshd

[maxwell@station maxwell]$ pgrep -lu maxwell 2112 bash

2146 mozilla-bin

2155 mozilla-bin

2156 mozilla-bin

2157 mozilla-bin

Linux+ Guide to Linux Certification, 2e

14

Filtering ps command Output See example page 438

Output from a ps –ef listing can be quite long By piping the output of the ps command through grep you can search for the

specific process you want to terminate and determine the correct PID

Linux+ Guide to Linux Certification, 2e

15

Chapter 2 Key Concepts In Linux, the first process, /sbin/init, is started by the kernel on

bootup. All other processes are the result of a parent process duplicating itself, or forking.

A process begins executing a new command through a process called execing.

Often, new commands are run by a process (often a shell) first forking, and then execing. This mechanism is referred to as the fork and exec mechanism.

Processes can always be found in one of five well defined states: runnable, voluntarily sleeping, involuntarily sleeping, stopped, or zombie.

Process ancestry can be viewed with the pstree command. When a process dies, it is the responsibility of the process's parent

to collect it's return code and resource usage information. When a parent dies before it's children, the orphaned children are

inherited by the first process (usually /sbin/init).

Linux+ Guide to Linux Certification, 2e

16

How Linux Manages Processes page 435 During the boot, the first 2 main system processes are started

sched (scheduler) (pid 0) init (initialization) (pid 1) which manages all other processes

Figure 10-2: Process genealogy

Linux+ Guide to Linux Certification, 2e

17

One process can start another process

All Processes require a unique ID within the system. PID = Unique identifier assigned to a process

Child process: (PID): Process started by another process

Parent Process ID (PPID): Process that has started another process

Figure 10-1: Parent and child processes

Linux+ Guide to Linux Certification, 2e

18

There are different types of processes Daemon

System processes that exist for a specific system purpose httpd daemon - exists for the sole purpose of handling internet services, running

inactive in the background until needed

Parent A process which has spawned another process Following boot-up, a process called init is invoked Every process, except init, has a parent process

Child process spawned by another process Except for init all other processes are a child processes of a parent process

Linux+ Guide to Linux Certification, 2e

19

Types of (troubled) Processes Orphan

Created when a child process is running and the parent process is killed. The system recognises the orphaned child process. And passes the orphan process to init which then becomes the it’s new

parent process and terminates it.

Zombie (or Defunct) Created when a child process loses it’s connection to an existing parent process. It keeps running but becomes “lost” in the system And so cannot be terminated by it’s parent process. It just keeps running forever. Keeps using system resource by using a slot in the process table; It cannot be stopped in a conventional manner The only way to kill a zombie is to reboot the system

Linux+ Guide to Linux Certification, 2e

20

Process states ps -aux Current processor state of process Most processes are either sleeping (S) or running (R)

Zombie process state is Z

Process state

Description

S If a process is not being run on the processor at the current time, then you’ll see an S (sleeping) in the process state column.

R Process is currently running on the processor.

T Process has stopped or is being traced by another process.

Z This indicates a zombie process.

Linux+ Guide to Linux Certification, 2e

21

/proc The ps command gets it’s info from /proc

/proc is a system directory created at installation It is always created under the / directory It is similar to the windows device manager

It holds system information. The process information for each processes is stored in the /proc system directory

using it’s PID. The ps commands is used to view this information

Linux+ Guide to Linux Certification, 2e

22

Viewing “Top” Processes top command:

Displays interactive screen listing processes Organized by processor time Processes using most processor time listed first

Identify Rogue process: Faulty process Consumes excessive system resources

top command can be used to change a processes priority or terminate a processes.

Linux+ Guide to Linux Certification, 2e

23

Chapter 3 - Process Scheduling: nice and renice Key Concepts A primary task of the Linux kernel is scheduling processes. Every process has a niceness value that influences its

scheduling. The nice and renice commands can change a process's

scheduling priority.

Linux+ Guide to Linux Certification, 2e

24

Process priority & niceness Every process has two values which influence its scheduling. The first is a dynamic value which is constantly being

changed by the kernel as the process's priority. The second is a fixed value, which is only occasionally

(if ever) explicitly changed by a user is the process's niceness.

nice command: Change a process’s niceness as it starts

nice +13 vi myfile.txt

renice command: Alter niceness of a process after it is running Only root user may change NI to a negative value nice 19 1426

Linux+ Guide to Linux Certification, 2e

25

Setting Process Priority (PRI) Nice value (NI): Indirectly represents priority

Determines how many processor time slices the process will receive. Higher value means lower priority Higher value means lower priority

EXAMPLE

nice +13 vi myfile.txt runs the vi command on myfile.txt with an increment of +13.

Linux+ Guide to Linux Certification, 2e

26

Chapter 4 - Sending Signals

Key Concepts Signals are a low level form of inter-process communication, which arise from a

variety of sources, including the kernel, the terminal, and other processes. Signals are distinguished by signal numbers, which have conventional symbolic

names and uses. The symbolic names for signal numbers can be listed with the kill -l command.

The kill command sends signals to other processes. Upon receiving a signal, a process may either ignore it, react in a kernel

specified default manner, or implement a custom signal handler. Conventionally, signal number 15 (SIGTERM) is used to request the termination

of a process. Signal number 9 (SIGKILL) terminates a process, and cannot be overridden. The pkill and killall commands can be used to deliver signals to processes

specified by command name, or the user who owns them. Other utilities, such as top and the GNOME System Monitor can be used to

deliver signals as well.

Linux+ Guide to Linux Certification, 2e

27

It is often necessary to kill parent process in order to kill a child process

Killing a parent process will kill all child processes spawned by it. Such as if you wished to kill all the spawned child processes.

Use the output from the ps command. Look at the output from ps command and use it read the PID and PPID information. Tracing from the child process up the hierarchy to the parent processes.

Linux+ Guide to Linux Certification, 2e

28

Terminating processes You can terminate a process using the kill, killall, top See examples p444

kill command: Kills all instances of a specific process

By PID Or by command name

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

Linux+ Guide to Linux Certification, 2e

29

Kill Signals kill –l

Displays a list of all the possible kill signals. 64 different kill signals Affect processes in different ways

Table 10-2: Common administrative kill signals

Linux+ Guide to Linux Certification, 2e

30

Kill Signals

kill –signal command: To kill a process, give kill signal and PID

If no kill signal given, SIGTERM ( 15) assumed

$ kill -15 soft kill

If process will not terminate SIGKILL ( 9 ) can be used to force it.

$ kill -9 absolute kill

Linux+ Guide to Linux Certification, 2e

31

Chapter 5. Job Control

Key Concepts The bash shell allows commands to be run in the background as

"jobs". The bash shell allows one job to run in the foreground, and can

have multiple backgrounded jobs. The jobs command will list all backgrounded jobs. The CTRL-Z key sequence will suspend and background the

current foreground job. The bg command resumes a backgrounded job. The fg command brings a backgrounded job to the foreground.

Linux+ Guide to Linux Certification, 2e

32

Running Processes There are 2 areas in which processes can be run.

Foreground processes: BASH shell must wait for process termination to continue

Background processes: BASH shell does not wait for termination to continue Processes can be run in the background by appending an & to

the command name When “ & “ is placed at the end of a command it causes the command to be

run in the background upon execution. The user receives a BASH shell prompt immediately.

Linux+ Guide to Linux Certification, 2e

33

Background /Foreground processes See examples p447 jobs command:

Lists background processes running in current shell Each background jobs is given a specfic “ %# “ by which it is referred. It also marks the two most recent background processes

Commands operate on most recent process, by default + symbol = most recent process - symbol = next most recent process

foreground (fg) command: Run move a background process into the foreground fg %1

background (bg) command: To run a job in the background place a “ & “ at the end of the command-line

vi my-document & Or type “ ctrl Z” to move a running foreground process into the background

Linux+ Guide to Linux Certification, 2e

34

Chapter 6. Scheduling Delayed Tasks: at Key Concepts The at command can submit commands to run at a later time. The batch command can submit commands to run when the

machines load is low. Commands can either be entered directly, or submitted as a script. stdout from at jobs is mailed to the user. atq and atrm are used to examine and remove currently scheduled

jobs.

Linux+ Guide to Linux Certification, 2e

35

Scheduling Commands Commands can be scheduled to run at a later time Using the at or cron daemons.

at daemon (atd): System daemon that executes tasks at a future time

cron daemon (crond): System daemon that executes tasks repetitively in the future

Linux+ Guide to Linux Certification, 2e

36

Scheduling Commands with atd See examples page 452 - 455

at <time> command: Used to schedule commands to run at a preset time. Runs command once only. Example:

at now

cp /etc/passwd ~/atpass

Table 10-3: Common at commands

Linux+ Guide to Linux Certification, 2e

37

Viewing Scheduled “ at “ Jobs Shell environment & scheduled commands are stored in /var/spool/at

Used to allow us to view scheduled jobs queue. The “ at “ daemon uses current shell’s environment for execution If stdout of scheduled command has not been redirected to file, It is mailed to user

atq command:

Each local users has their own “ at “ queue. Regular users can only see their own jobs.

–l option: View the queued list of scheduled jobs –c option: View system environment at scheduling time –d option: Delete a job –f option: Run scheduled jobs from shell script

Linux+ Guide to Linux Certification, 2e

38

Chapter 7. Scheduling Periodic Tasks: cron Key Concepts The cron facility is used to schedule regularly recurring tasks. The crontab command provides a frontend to editing crontab

files. The crontab file uses 5 fields to specify timing information.

stdout from cron jobs is mailed to the user.

Linux+ Guide to Linux Certification, 2e

39

Scheduling Commands with crond See examples page 457 – 459

crontab command: Used to schedule commands on a preset schedule. Local users can view and edit their own cron tables Six fields separated by space or tab characters which determine action

–e option: Edit cron tables in vi editor –l option: List a user cron table –r option: Remove all scheduled jobs

Linux+ Guide to Linux Certification, 2e

40

Example crond table entry

Figure 10-6: Sample user cron table entry

Linux+ Guide to Linux Certification, 2e

41

Summary ps ps –f -e ps –ef | grep PID kill killall jobs fg bg kill %1 nice renice at crontab