Home > Unix > UNIX – Lesson 025 – Job and Job Scheduling – Foreground, Background and jobs command

UNIX – Lesson 025 – Job and Job Scheduling – Foreground, Background and jobs command


Foreground vs. Background
A foreground job can receive keyboard input and signals such as Control-C from the controlling terminal, background jobs cannot. If the login session is disconnected, foreground jobs are terminated by a hang-up signal, while backgrounds jobs are not. Both foreground and background jobs can write to the controlling terminal.

Putting a Job in the background
Jobs can be put in the background either by initiating them in the background or by stopping a foreground job and then specifying that it be continued in the background.
The way a job is initiated in the background is by putting an ampersand (&) at the end of the command line. For example, to start the excellent web browser Mozilla, if you plan on using the terminal for other activities while the web browser is running, you can type:

$ mozilla &
A second technique is to stop the foreground job with control-Z, and then to specify that the job be continued in the background with the “bg” command (The Carrot, ^, represents holding down the control key while you press the letter after it).
So, if you start Mozilla

mozilla
^Z
bg

When you run in the background a program that uses a graphical user interface for interaction, you will still be able to interact with its graphical elements in your windowing system. If you suspend it with ^Z, you will not be able to interact with it until you start it running, either in the foreground (fg) or the background (bg).

==================================================================================

jobs

The “jobs” command displays the jobs that you are running in the background and in the foreground. If the prompt is returned with no information no jobs are present.
Note: not all shells are capable of running this command.

Key argument:

-l       Reports the process group ID and working  directory of the jobs

Example :
$ xclock &
[1] 12018

$ find / -name “*.c” -print &
[2] 16619

$ jobs
[1] + Running  xclock
[2] + Running   find / -name “*.c” -print &

$ jobs -l

[1]  +  7533 Running   xclock
[2]  + 7534 Running   find / -name “*.c” -print &

$ kill %2
In order to kill the second job.

$ jobs
[1] + Running  xclock
[2] + Terminated   find / -name “*.c” -print &

Categories: Unix
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: