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
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).
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.
-l Reports the process group ID and working directory of the jobs
$ xclock &
$ find / -name “*.c” -print &
 + Running xclock
 + Running find / -name “*.c” -print &
$ jobs -l
 + 7533 Running xclock
 + 7534 Running find / -name “*.c” -print &
$ kill %2
In order to kill the second job.
 + Running xclock
 + Terminated find / -name “*.c” -print &