0% found this document useful (0 votes)
11 views16 pages

LX 132 VB 11

Unit 11 covers controlling processes in Linux, including monitoring, invoking, and terminating processes using commands like ps, kill, and nohup. It explains foreground and background processes, job control in the bash shell, and the role of daemons in managing system resources. Key concepts include process hierarchy, signals for termination, and managing process priorities with nice and renice.

Uploaded by

mhmdfoad66
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views16 pages

LX 132 VB 11

Unit 11 covers controlling processes in Linux, including monitoring, invoking, and terminating processes using commands like ps, kill, and nohup. It explains foreground and background processes, job control in the bash shell, and the role of daemons in managing system resources. Key concepts include process hierarchy, signals for termination, and managing process priorities with nice and renice.

Uploaded by

mhmdfoad66
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Unit 11 Controlling Processes

Course code LX13


Linux Basics
Objectives

After completing this unit, students should be able to:


Describe process monitoring
Invoke background processes
Terminate processes
List useful signals
Use the nohup command
Control jobs in the bash shell
Define system processes
Monitoring Processes
The ps command displays process status information
$ ps -jf
PPID PID ... TTY STAT UID TIME COMMAND
360 374 ... 1 S 500 0:00 bash
374 569 ... 1 S 500 0:00 bash
569 572 ... 1 R 500 0:01 find /
569 575 ... 1 R 500 0:00 ps -jf

ps has a number of command line options:


u shows user names instead of UID
a shows all processes
Viewing Process Hierarchy

pstree shows process hierarchy


$ pstree
init-+-apmd
|-atd
|-crond
|-dhcpd
|-gpm
|-httpd---10*[httpd]
|-inetd
|-kattraction.kss
|-kdm-+-X
| `-kdm---kwm-+-kbgndwm
| |-kfm
| |-kpanel
| |-krootwm
| |-kvt---bash---man---apropos---less
| |-kvt---bash---script---script---bash---pstree
| |-kvt---bash---su---bash
| |-kvt---bash---man---sh-+-gunzip
| | `-less
| `-startkde---autorun
|-kflushd
...
Controlling Processes

Foreground Processes
bash (wait)

ls -l
Foreground processes are invoked by simply typing a
command at the command line.

Background Processes
bash

ls -l &
Background processes are invoked by putting an "&" at
the end of the command line
Terminating Processes

Foreground process
ctrl-c Interrupt key, cancels a foreground
process. After the interrupt, the
system returns the prompt on the
screen
kill Sometimes the kill command is
used to terminate foreground
processes

Background process
kill The kill command is the only
way to terminate background
processes
Terminating Processes

The kill command sends a signal to a running process, which


normally stops the process.

$ ps -jf
PPID PID ... TTY STAT UID TIME COMMAND
122 374 ... 1 S 500 0:00 bash
374 569 ... 1 S 500 0:00 bash
569 572 ... 1 R 500 0:01 find /
569 575 ... 1 R 500 0:00 ps jf

$ kill 572
Signals

Signal Meaning
01 hangup - you logged out while the process
was still running (ctrl-d)

02 interrupt - you pressed the interrupt key


ctrl-c
03 quit - you pressed quit key sequence ctrl-\
09 software terminate - the most powerful
signal that can be sent, this cannot be
avoided or ignored
15 software terminate process terminated by
kill command (default)
Running Long Processes

The nohup command will stop a process from being


killed if you log off the system before it completes

$ nohup ls -R / >out &


[1] 391

If you do not redirect output, nohup will redirect output to


a file nohup.out
$ nohup ls -R / &
[1] 393
nohup: appending output to 'nohup.out'
Managing Process Priorities

Processes are scheduled according to priority


Priority range from -20 (highest) to 19 (lowest)
Only the superuser can set priorities < 0
Default priority: 0
Use nice to set the priority when starting a process
$ nice ls -R / >out &
[1] 1894
$ ps -l
... PID PPID ... NI ... CMD
857 854 0 bash
1894 857 10 ls
1895 857 0 ps
Use renice to change the priority of a running process
$ renice 20 1894
1894: old priority 10, new priority 20
Job Control in the Bash Shell

<ctrl-z> suspends foreground task

jobs lists background or suspended jobs

fg resume suspended task in the


foreground

bg resume suspended task in the


background

Specify a job number for bg, fg and kill using %job


Job Control Example

$ ls -R / >out 2>out.error &


[1] 402

$ jobs
[1]+ Running ls -R / >out 2>out.error &

$ fg %1
ls -R / >out 2>out.error &

<ctrl-z>
[1]+ Stopped ls -R / >out 2>out.error &

$ bg 402 ; jobs
[1]+ Running ls -R / >out 2>out.error &

$ kill %ls
Daemons

A daemon is a never-ending process, usually a system


process that controls a system resource such as the
printer queue.

Print Queue lpd

print job 1

print job 2

print job 3

/dev/lp0
Checkpoint

1. What option would you use with ps to show the detailed


commands you are running?

2. What limitations are there to running the kill command?

3. What is the strongest signal that can be sent to a


process to terminate it?
Checkpoint (2)

4. What is the name for special never-ending system


processes in the UNIX environment?
Unit Summary

To monitor processes use the ps command


Background processes are invoked by including an
ampersand (&) at the end of the command
Use the kill command to terminate processes
Some usefull signals that terminate processes are -2, -3
and -9
Jobs can be controlled in the bash shell by suspending a
job with <ctrl-z> and restarted using the bg or fg
command
The nohup command allows you to start a job in the
background and complete processing after you log off
System processes are called daemons. They are often
used to control system resources like the printer queueing
mechanism

You might also like