I'm studying Redhat Linux 7, topic Monitoring and managing Linux processes in it I understand Processess and its command. But I do not understand "Controlling Jobs" and Running jobs in the background and its practical its background and Foreground processes. Can you explain this term to us?
3 Answers
Most commands issued from an interactive shell run in foreground. That basically means that you must wait for the executed command (or processus ) to stop before doing something else. For long/complex programs or scripts, the alternative is to run them in the background. This means that you can continue to work while the long program executes.
The ampersand &
at the end of a command does this. You can also use ctrl-Z to suspend a foreground command and throw it in the background with the bg
command.
You can thereafter manage these backgrounds tasks (jobs
command), kill them, etc.
Please note that the background command is not detached from your tty : there may be some cases when a background command waits for user input (see fg
command to bring back a job to foreground). If you end your terminal session (through closing the terminal, logout, shutdown, etc.), background jobs may be also killed (see nohup
for more details).
-
Re: that last paragraph: if you run a command in the background from your terminal, and then close that terminal and/or session, then that job gets killed in the same way as if you were to logout or shut the computer down.– jvriesemJun 16, 2017 at 18:29
-
@jvriesem Indeed. I adjusted the answer to be more precise regarding end of session scenarios.– UrielJun 17, 2017 at 21:40
in simple words,
Every process that you start runs in the foreground. It gets its input from the keyboard and sends its output to the screen.
$ls ch*.doc
which list all the files in current directory,
A background process runs without being connected to your keyboard. If the background process requires any keyboard input, it waits.The advantage of running a process in the background is that you can run other commands; you do not have to wait until it completes to start another!
The simplest way to start a background process is to add an ampersand ( &) at the end of the command.
$ls ch*.doc &
some below links will help you
https://stackoverflow.com/questions/8938058/background-process-vs-foreground-process
http://linux.about.com/cs/linux101/a/multitasking.htm
https://stackoverflow.com/questions/6573249/difference-between-foreground-job-and-background-job
Running a program in the background by adding &
at the end is like running an application minimized in Windows or Mac. And using the fg
command is like restoring it back to the desktop.
In comparison, when you press ctrl+z
while a program is running will suspend its execution. Which means it will be stopped from completing what it is doing and sent to the background.
Furthermore, the programs are tied to your current session so if you exit your terminal all the programs running or suspended in the background will end too.