As a Linux system administrator you may sometimes want to run process in background to continue working on your command while the background process finishes its work. Linux system allows for a simultaneous process execution and ability to run programs in foreground, background. This tutorial will teach you some basic ins and outs of the foreground and background bash shell feature.

By default any process started on the bash command line is run in the foreground which consumes your command prompt. As an example start yes command by redirecting it standard output to /dev/null:
$ yes > /dev/null 

After execution of the above command your shell command line becomes unresponsive since it is entirely dedicated to you "yes" process. From here you have two options. Either terminate this process by CTRL+C key combination or or stop the process using CTRL+Z. Let's stop the process:
$ yes > /dev/null 
[1]+  Stopped                 yes > /dev/null

After pressing CTRL+Z combination our process have stopped and we have regained a control of you command line. By running a jobs command we can confirm process status:
$ jobs
[1]+  Stopped                 yes > /dev/null
The output of jobs command shows the process job number [1], state "Stopped" and the actual process name "yes > /dev/null". User able able to start processes directly in the background using "&" sign. Let's start another jobs in the background:
$ yes > /dev/null &
[2] 20126
$ jobs
[1]+  Stopped                 yes > /dev/null
[2]-  Running                 yes > /dev/null &
Currently, jobs command reports two processes in our job list queue. To continue execution of job [1] in background we can use bg command:
$ bg %1
[1]+ yes > /dev/null &
$ jobs
[1]-  Running                 yes > /dev/null &
[2]+  Running                 yes > /dev/null &
The "+" sign next to the job number indicates the current job, that is the last job stopped while it was in the foreground or started in the background whereas the previous job is always marked with "-" sign. Therefore we can refer to current job as "%" or "%+" and previous job by "%-". The following command will put process [2] in the foreground.
$ fg %
yes > /dev/null
[2]+  Stopped                 yes > /dev/null

Free Linux eBooks

Do you have the right skills?

Our IT Skills Watch page reflects an up to date IT skills demand leaning towards the Linux and Unix environment. We have considered a number of skills and operating systems.

See the result...

Linux Online Training

Learn to run Linux servers and prepare for LPI certification with Linux Academy. 104 available video lessons with PDF course notes with your own server!

Go to top