Job control :
Job control refers to
the ability to selectively stop ( suspend ) the execution of
processes and continue ( resume ) their execution at a later point .
It keeps a table of currently executing jobs, which may be listed
with the
jobs
command .
To facilitate the
implementation of the user interface to job control , the system
maintains the notion of a current terminal process group ID . Members
of this process group receive keyboard-generated signals such as
SIGINT
.
If the operating
system on which Bash is running supports job control , Bash allows
you to use it . You may then manipulate the state of this job ,
using the
bg
command to continue it in
the background , the fg
command to
continue it in the foreground , or the kill
command to kill it .
The character `%'
introduces a job name . Job number
n
may be referred to as `%n'.
The symbols `%%'
and `%+' refer to the shell's notion of
the current job , which is the last job stopped while it was in the
foreground . Simply naming a job can be used to bring it into the
foreground: `%1' is a synonym for `fg
%1' bringing job 1 from the background into the foreground .
Similarly, `%1 &' resumes job 1 in the background, equivalent to `bg %1' . You may then use the
jobs
command to inspect their status .
JOB CONTROL COMMANDS :
Creating your first Linux/Unix job
I am going to run a command called xeyes that displays two googly eyes on screen, enter:
$ xeyes &
Sample outputs:
Fig.01: Running the xeyes command in the background
I started a job in the background with an ampersand (&). The shell prints a line that looks like the following:
[1] 6891
In this example, two numbers are output as follows
- [1] : The xeyes job, which was started in the background, was job number 1.
- 6891 : A process ID of job number
1.
I am going to start a few more jobs:
## Start a text editor, system load average display for X, and sleep command ## gedit /tmp/hello.c & xload & sleep 100000 &
#2: List the current jobs
To see the status of active jobs in the current shell, type:
$ jobs
$ jobs -l
Sample outputs:
[1] 9379 Running xeyes & [2] 9380 Running gedit /tmp/hello.c & [3]- 9420 Running xload & [4]+ 9421 Running sleep 100000 &
The fg command switches a job running in the background into the foreground . The bg command restarts a suspended job , and runs it in the background .
If no job number is specified, then the fg or bg command acts upon the currently running job .
EXAMPLE FOR fg & bg COMMAND :
Resume suspended/stopped job in the foreground
Let us resume or bring stopped ping job to the foreground and make it the current job with the help of fg command. The syntax is as follows:
## Job id number 5 for ping command ## fg %5 |
I can also state any job whose command line begins with the string "ping":
## %String ## fg %ping |
64 bytes from www.cyberciti.biz (75.126.153.206): icmp_req=3 ttl=53 time=265 ms 64 bytes from www.cyberciti.biz (75.126.153.206): icmp_req=4 ttl=53 time=249 ms 64 bytes from www.cyberciti.biz (75.126.153.206): icmp_req=5 ttl=53 time=267 ms ^C
#5: Resume suspended/stopped job in the background
In this example , I am going to update all installed packages on Red Hat or CentOS Linux production server using yum command background job:
# yum -y update &>/root/patch.log
& |
However, due to some reason (say load issue) I decided to stop this job for 20 minutes:
# kill -s stop %yum |
Sample outputs:
[7]+ Stopped yum -y update &>/root/patch.log &
Restart a stopped background yum process with bg
Now, I am going to resume stopped the yum -y update &>/root/patch.log & job, type:
# bg %7 |
OR
# bg %yum |
Sample outputs:
[7]+ yum -y update &>/root/patch.log &
No comments:
Post a Comment