|
|
A job is a running process in memory. Job control is a shell feature that moves jobs from the foreground to background, suspends running jobs, and restarts
Whenever a
To start a job in the background you can put the
&
symbol at the end of your command prompt. Usually, the jobs that don't need any interactive input are started in the background. You may also run programs in the background that need input by redirecting stdin to some file on the disk. If a background job sends its output to
$
ll /usr >mylist &
[1] 11633
$
Once a job is started in the background, the shell prints the job ID and
process ID
(PID)
$
jobs
[1] + Stopped vi myfile
[2] - Running vi file2
$
The jobs command lists all background jobs, job IDs, and whether these jobs are running or stopped. A plus symbol ( + ) in the output of this command shows that this is the current job; a minus sign ( - ) shows which job is scheduled to be run next after the current job. A -l switch with the jobs command shows the PID of all jobs.
Jobs may be in a running or stopped state. A job may be stopped if it needs input that is not available or is suspended by the user. When a job is completed, it goes into the
Done
state. If you run a job in the background and it
Many times you start a job in the foreground and then you want to do something else without abandoning the program running in the foreground. For example, you may have started the
vi
editor and you need to copy files without abandoning the editor. The POSIX shell provides a mechanism to suspend a current job temporarily. To suspend a running job, you can use the key sequence represented by the
susp
value in the
stty -a
output, often defined as
. Pressing that key sequence suspends the current job and gives you the command prompt. The job is
suspended,
and if you use the
jobs
command, you will see the job is stopped. You will also see a plus (
+
) symbol showing that this is the current job.
$
jobs
[1] + Stopped vi myfile
[2] - Stopped vi file2
$
To find the value of the
susp
sequence, use the
stty -a
command. If this value is not set, you can use the
stty
command to set its value as
$
stty susp ^z
$
The (
^
) symbol shows the control key. After this command, you can use the
key sequence to suspend a job.
All suspended jobs can be resumed with the foreground ( fg ) command. The same command is used to bring background jobs to the foreground. To bring a job to the foreground, you need to know the job id with the jobs command. If you don't specify any job ID, the current job is brought into the foreground. For example, when you suspend the vi editor to do some other work, the fg command will bring the vi screen back, and you can use it again in the normal way.
$ jobs [1] + Stopped vi myfile [2] - Stopped vi file2 $ fg %2
Job numbers are used with the percent (
%
) symbol with the
fg
command. You can also use the command
Study BreakJob Control
|
As soon as you suspend a foreground job, it goes into a stopped state. You can start the job, keeping it in background with the bg (background) command. To send a foreground job into the background, first suspend it and then use the bg command. If there is more than one suspended job, you need to provide a job ID to the bg command to bring a particular job into running state from stopped state. The following sequence of commands lists background jobs and then changes the state of job number 2 to running.
$ jobs [1] + Stopped vi myfile [2] - Stopped vi file2 $ bg %2 [1] + Stopped vi myfile [2] - Running vi file2 $
There is no direct way to stop a running job. We can, however, adopt an alternate method to stop a running job. We can bring a background job into the foreground and then suspend it. You may need to stop a running job temporarily when it is taking a lot of system resources and you want to run a more important job.
At any point, if you want to wait for background jobs to be finished, just use the
wait
command. This command stops the command prompt until all background jobs are finished. If you want to stop the
wait
command, just press the
key on your keyboard. We can also wait for a particular job to finish by specifying the job number as an argument to the
wait
command.
$
wait %2
|
|
|
|
| Top |