Managing Bash Processes in the Foreground and Background

When you run a typical command from the shell, it will be run in the foreground.  You have to wait for the process to complete before you can type another command.  Some processes take time to complete and you can’t do anything until that process finishes.  This always happens to me.  I start something that runs longer than anticipated and need to leave.  Grr!  Can’t go until it’s done.  Well, that’s not entirely true.  You can switch the process to the background and then back to the foreground again if you wish.  This article will teach you about managing bash processes in the foreground and background.

Tools for Managing Processes

Since you will be working with processes in the foregound and the background, you will need to have a few tools.  Here are the commands that you will need:

Command Description
<ctrl-z> interrupts the current process running in your shell
bg <number> bg starts the process you specify by number into background processing.
fg <number> fg will bring the process you specify by number into foreground processing as the current process.
jobs (-l) %<number> the jobs command will show all a list of all of the processes that your userid has running.  Using the -l option, will also list the PID.  Using the jobs command along with a %+number will show you only the job number you specified.
disown <number>disown -adisown -h The disown command with the job number will disconnect that job number from the current shell.  The job will continue to run but will no longer be associated with the current shellThe disown -a command will disassociate all of the jobs from current shellDisown -h will offer protection to the jobs from signals (SIGHUP).  If a signal is sent to the shell, it will not be sent to the job itself.

Bash Shell Foreground and Background Processes

Let’s try some examples of managing processes in the foreground and background using the tools ( disown, jobs, bg, fg, etc…) that we listed above.

Example – Start 3 processes and place them all into background processing

(First, let's launch, interrupt it, run it in the background)

$ ./
[1]+ Stopped ./
$ bg 1
[1]+ ./ &

(Second, let's launch, and interrupt them, and then run them in the background)
$ ./
[2]+ Stopped ./
$ bg 2
[2]+ ./

$ ./
[3]+ Stopped ./
$ bg 3
[3]+ ./

Note how when we interrupted each of the processes, it listed the process number in brackets.

Example – Display the jobs currently running

$  jobs -l
[1] 8273 Running ./ &
[2]- 8305 Running ./ &
[3]+ 8345 Stopped ./

Example – Bring job #2 back into the foreground

# fg 2

( Job #2 is now running in the foreground )

Example – Disassociate job #1 from the current shell

(First, list all currently running jobs)

$ jobs -l
[1] 8273 Running ./ &
[2]- 8305 Running ./ &
[3]+ 8345 Stopped ./

(Looks like we have 3 jobs running [ 1,2,3 ].  Let's disown job #1)

$ disown %1

(Now that we have disowned job #1, let's see if it is still associated in our list)

$ jobs -l
[2]- 8305 Running ./ &
[3]+ 8345 Stopped ./

(Let's verify that our disowned job [ PID 8273 ] is still running)

$ ps -ef | grep 8273
root 8273 25753 0 04:01 pts/0 00:00:00 -bash
root 15596 8273 0 04:22 pts/0 00:00:00 sleep 5
root 15598 25753 0 04:22 pts/0 00:00:00 grep 8273

(Sure enough, it is.  Our disowned job is still running, even though it isn't owned by our shell anymore.)

Well, that’s it.  You should be adept at switching processes from the foreground, to the background and to the foreground again.  You can also list jobs that are running and disown them so that you can close your shell and leave when needed!

The following two tabs change content below.
Jeff has 20 years of professional IT experience, having done nearly everything in his roles of IT consultant, Systems Integrator, Systems Engineer, CNOC Engineer, Systems Administrator, Network Systems Administrator, and IT Director. If there is one thing he knows for sure, it is that there is always a simple answer to every IT problem and that downtime begins with complexity. Seasoned IT professional by day, Jeff hopes to help other IT professionals by blogging about his experiences at night on his blog: You can find Jeff on or LinkedIn at: LinkedIn or Twitter at: Twitter

Latest posts by Jeff Staten (see all)

Leave a Reply

Your email address will not be published. Required fields are marked *