Anda di halaman 1dari 44

Chapter 8

Process in Linux System

VIEW AND MANAGE PROCESSES

To manage processes you need to know the following:


Understand Process Definitions
Learn Jobs and Processes
Manage Foreground and Background Processes
View and Prioritize Processes
End a Process
Understand Services (Daemons)
Manage a Daemon Process

UNDERSTAND PROCESS DEFINITIONS

The following terms are used to describe Linux


processes:
ProgramA structured set of commands stored in an
executable file on a Linux file system
ProcessA program that is loaded into memory and
executed by the CPU
User processA process launched by a user that is started
from a terminal or within the graphical environment
Daemon processA system process that is not associated
with a terminal or a graphical environment

Figure 10-2 Relationship between daemon processes and user processes

UNDERSTAND PROCESS DEFINITIONS


(CONTINUED)

Properties of a process:

Process ID (PID)
Child process
Parent process
Parent Process ID (PPID)

UNDERSTAND PROCESS DEFINITIONS


(CONTINUED)

Figure 10-3 The relationship between parent and child process ID numbers

LEARN JOBS AND PROCESSES

Job identifier (job ID)

A numeric value that identifies the running program


uniquely within that shell

Each process is identified using a process ID (PID) that


is unique across the entire system
All jobs have a PID, but not all processes have a usable
job ID
PID 1 always belongs to the init process
When performing tasks such as changing the priority
level of a running program, use the PID

MANAGE FOREGROUND AND BACKGROUND


PROCESSES

Processes executed in the foreground

Background process execution

Started in a terminal window and run until the process is


completed
Occurs when a process is started and the terminal window
returns to a prompt before the process finishes executing

Existing processes can be switched from foreground to


background execution

MANAGE FOREGROUND AND BACKGROUND


PROCESSES (CONTINUED)
Commands in a shell can be started in the foreground
or in the background
Continue running a stopped process in the background
by entering bg
Appending an ampersand to a command starts the
process in the background
Each process started from the shell is assigned a job ID
by the job control of the shell
Switch a process to the foreground by entering fg
job_ID

VIEW AND PRIORITIZE PROCESSES

ps

View running processes with the ps (process status)


command

10

Table 10-8 Some commonly used options with ps

VIEW AND PRIORITIZE PROCESSES


(CONTINUED)

11

Table 10-9 Some of the fields (columns) in the process list

VIEW AND PRIORITIZE PROCESSES


(CONTINUED)

Table 10-10 Values for the STAT process state

12

VIEW AND PRIORITIZE PROCESSES


(CONTINUED)

pstree

Displays a list of processes in the form of a tree structure


Gives you an overview of the hierarchy of a process

nice and renice


The nice command assigns a process a specific nice value
that affects the calculation of the process priority
The lower the value of the nice level, the higher the priority
of the process

13

VIEW AND PRIORITIZE PROCESSES


(CONTINUED)

nice and renice (continued)

The nice level is used by the scheduler to determine how


frequently to service a running process
Use the command renice to change the nice value of a
running process

top

Allows you to watch processes continuously in a list that is


updated in short intervals

Provides a real-time view of a running system

Can also be used to assign a new nice value to running


processes or to end processes
14

15

Figure 10-4 The output of the top command

END A PROCESS

kill and killall

The killall command kills all processes with an indicated


command name
The kill command kills only the indicated process

GNOME System Monitor

Start the GNOME System Monitor utility (Computer >More


Applications > GNOME System Monitor) to view and kill
processes

16

END A PROCESS (CONTINUED)

Table 10-13 The more commonly used kill signals

17

18

Figure 10-5 The GNOME System Monitor utility

END A PROCESS (CONTINUED)

19

Table 10-14 Information displayed by default on the Process tab

UNDERSTAND SERVICES (DAEMONS)

A service is also called a daemon

Process or collection of processes that wait for an event to


trigger an action on the part of the program

Network-based services create a listener on a TCP or


UDP port when they are started
Listener waits for network traffic to appear on the
designated port
When traffic is detected, the program processes the traffic
as input and generates output that is sent back to the
requester

20

MANAGE A DAEMON PROCESS


Daemons run in the background and are usually
started when the system is booted
Daemons make a number of services available

Daemons are terminal-independent processes, and are


indicated in the ps x TTY column by a ?

Two types of daemons are available:


Signal-controlled daemons
Interval-controlled daemons

21

MANAGE A DAEMON PROCESS (CONTINUED)

Each daemon has a corresponding script in /etc/init.d/

Table 10-15 Parameters used to control daemon scripts

Many scripts have a symbolic link in either the


/usr/sbin/ directory or the /sbin/ directory
Find configuration files for daemons in the /etc/
directory or in its subdirectories

22

MANAGE A DAEMON PROCESS (CONTINUED)

Some important daemons:


cronStarts other processes at specified times
cupsdThe printing daemon
sshdEnables secure communication by way of insecure
networks (secure shell)
syslog ngLogs system messages in the directory /var/log/

23

MANAGE LINUX PROCESSES


In this exercise, start and stop processes and change
their priorities
First, start and suspend xeyes, move it to the
background and foreground, and stop it
Then, start xeyes and set the priority of the running
program to a nice value of -5
Start a second xeyes with a nice value of 10

24

SCHEDULE JOBS

Automate jobs in Linux by doing the following:


Schedule a Job (cron)
Run a Job One Time Only (at)

25

SCHEDULE A JOB (CRON)


Schedule jobs to be carried out on a regular basis by
using the cron service (/usr/sbin/cron)
The service runs as a daemon

A file that contains the list of jobs is called a crontab

Checks once a minute to see if jobs have been defined for


the current time
A crontab exists for the entire system as well as for each
user defined on the system

The /etc/sysconfig/cron file contains variables for the


configuration of some scripts started by cron
26

SCHEDULE A JOB (CRON) (CONTINUED)

System jobs

Control system jobs with the /etc/crontab file

Table 10-16 Directories containing system jobs that will be run by cron

Can add lines to /etc/crontab, but do not delete the lines


added at installation
Information on the last time the jobs were run is kept in
the /var/spool/cron/lastrun/ directory

27

SCHEDULE A JOB (CRON) (CONTINUED)

User jobs

The jobs of individual users are stored in the


/var/spool/cron/tabs/ directory

In files matching the usernames

Users create their own jobs using crontab

Table 10-17 Options for the crontab command

28

SCHEDULE A JOB (CRON) (CONTINUED)

User jobs (continued)


Each line in a file defines a job
There are six fields in a line

Table 10-18 Fields in a crontab file

29

RUN A JOB ONE TIME ONLY (AT)

If you want to run a job one time only, use the at


command

To use at, make sure the atd service is started


(rcatdstart)

Two files determine which users can run this


command:
/etc/at.allow
/etc/at.deny

Can modify or create these text files


30

RUN A JOB ONE TIME ONLY (AT)


(CONTINUED)

If the /etc/at.allow file exists, only this file is evaluated

If neither of these files exists, only the user root can define
jobs with at

Example:

31

SCHEDULE JOBS WITH AT AND CRON


In this exercise, schedule jobs with at and cron
First, redirect the output of finger to /var/log/messages
three minutes from the current time
Then, schedule the same job for tomorrow at noon
Then, schedule a program to run tomorrow at 2:00
p.m., and afterwards remove the job
In the second part of the exercise, create a cron job as
a normal user that logs the output of finger to
~/users.log every minute

32

MANAGE RUNLEVELS
Managing runlevels is an essential part of Linux
system administration
In this objective, you learn what runlevels are, the role
of the program init, and how to configure and change
runlevels:

The init Program and Linux Runlevels


init Scripts and Runlevel Directories
Change the Runlevel

33

THE INIT PROGRAM AND LINUX


RUNLEVELS

The init program

The system is initialized by /sbin/init

Started by the kernel as the first process of the system

This process, or one of its child processes, starts all


additional processes
SIGKILL has no effect on init
The configuration file for init is /etc/inittab
Part of the configuration in /etc/inittab is the runlevel the
system uses after booting

34

THE INIT PROGRAM AND LINUX RUNLEVELS


(CONTINUED)

The runlevels

Runlevels define the state of the system

Table 11-1 The available runlevels

35

THE INIT PROGRAM AND LINUX


RUNLEVELS (CONTINUED)

Init configuration file (/etc/inittab)

Each line in the /etc/inittab file uses the following syntax:


id:rl:action:process

The first entry in the /etc/inittab file contains the following


parameters: id:5:initdefault:

The next entry in /etc/inittab looks like this:


si::bootwait:/etc/init.d/boot

The next few entries describe the actions for runlevels 0 to 6

36

INIT SCRIPTS AND


DIRECTORIES

RUNLEVEL

/etc/inittab defines the runlevel the system uses after


booting is complete
init scripts

The /etc/init.d/ directory contains shell scripts that are used


to perform certain tasks at bootup and start and stop
services in the running system
The shell scripts can be called up in the following ways:

Directly by init when you boot the system


Indirectly by init when you change the runlevel
Directly by /etc/init.d/script parameter

37

INIT SCRIPTS AND


(CONTINUED)

RUNLEVEL DIRECTORIES

Table 11-2 /etc/init.d/script parameters

38

INIT SCRIPTS AND RUNLEVEL


DIRECTORIES (CONTINUED)

init scripts (continued)

Some of the more important scripts stored in /etc/init.d/:


boot
boot.local
halt
rc
service

39

INIT SCRIPTS AND RUNLEVEL


DIRECTORIES (CONTINUED)

Runlevel symbolic links


To enter a certain runlevel, init calls the /etc/init.d/rc script
with the runlevel as a parameter
This script examines the respective runlevel /etc/init.d/rcx.d/
directory and starts and stops services depending on the
links in this directory
Each runlevel has a corresponding subdirectory in
/etc/init.d/

40

41

Figure 11-6 The YaST Runlevel Editor module

Figure 11-7 The YaST Runlevel Editor module in expert mode

42

INIT SCRIPTS AND RUNLEVEL


DIRECTORIES (CONTINUED)

Activate and deactivate services for a runlevel


(continued)

Normally, the default runlevel of a SUSE Linux system is


runlevel 5

Changes to the default runlevel take effect the next time you boot
your computer

To configure a service, select a service from the list

Then, from the options below the list, select the runlevels you want
associated with the service

43

CHANGE THE RUNLEVEL

Change the runlevel at boot

Possible to boot to another runlevel by specifying the


runlevel on the kernel command line of GRUB

Manage runlevels from the command line

Can change to another runlevel once the system is running


by using the init command

Like most modern operating systems, Linux reacts


sensitively to being switched off without warning
The shutdown command shuts down the system after the
specified time

44

Anda mungkin juga menyukai