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:
|<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 test1.sh, interrupt it, run it in the background) $ ./test1.sh <ctrl-z> + Stopped ./test1.sh $ bg 1 + ./test1.sh & (Second, let's launch test2.sh, and test3.sh interrupt them, and then run them in the background) $ ./test2.sh <ctrl-z> + Stopped ./test2.sh $ bg 2 + ./test2.sh $ ./test3.sh <ctrl-z> + Stopped ./test3.sh $ bg 3 + ./test3.sh
Note how when we interrupted each of the processes, it listed the process number in brackets.
Example – Display the jobs currently running
$ jobs -l  8273 Running ./test1.sh & - 8305 Running ./test2.sh & + 8345 Stopped ./test3.sh
Example – Bring job #2 back into the foreground
# fg 2 ./test2.sh ( Job #2 is now running in the foreground )
Example – Disassociate job #1 from the current shell
(First, list all currently running jobs) $ jobs -l  8273 Running ./test1.sh & - 8305 Running ./test2.sh & + 8345 Stopped ./test3.sh (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 - 8305 Running ./test2.sh & + 8345 Stopped ./test3.sh (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!