Running commands in the Linux terminal is likely something you do nearly every day, but moving a running process into the “background” and later moving it back into the “foreground” is something different altogether. When a process is running in the foreground (the usual state of running commands), it could be said to “own your terminal”. In other words, you will likely sit and wait for it to finish before you even try to run another command. If a process is likely to take a long time to complete, on the other hand, you do have an option other than waiting patiently for it to get done. You can move it into the background – freeing up your terminal window to run other commands.
Moving a process to the background
To move a process into the background, you would type ^z (hold the Ctrl key and press “z”) to suspend it, and then type bg to move it into the background. The process will keep running, but you’ll get your prompt back and be able to work on something else while it’s still running to completion in the background. Note that a backgrounded process will keep running until it’s done or until you log off. Here’s an example:
$ sleep 1000 ^Z [1]+ Stopped sleep 1000 $ bg [1]+ sleep 1000 &
Checking on backgrounded processes with the jobs command
To list backgrounded processes, use the jobs command like this:
$ jobs [1]+ Running sleep 1000 &
Note that the backgrounded processes will have a job number (1 in this case).
Moving a process back into the foreground
To move a backgrounded process back into the foreground, use the fg command (if there’s only one) or the fg command followed by the job id to specify a particular process. It will take over your terminal window.
$ fg sleep 1000
You can then wait for the process to finish or move it again into the background.
Going between foreground and background
In the example scenario below, we start two processes and stop each with a ^z.
$ sleep 1000; sleep 2000 ^Z [1]+ Stopped sleep 1000 ^Z [2]+ Stopped sleep 2000
We then background both of the processes with a bg command including the job numbers.
$ bg 1; bg 2 [1]- sleep 1000 & [2]+ sleep 2000 & $ jobs [1]- Running sleep 1000 & [2]+ Running sleep 2000 &
The step below shows moving only the second backgrounded process back into the foreground.
$ fg 2 sleep 2000
Running a process in the background from the start
If you want to run a command in the background from the start, add an asterisk to the end of the command (e.g., runme &). This will put the process immediately into the background. After that, you can run whatever other commands you want to run and check on the backgrounded process as needed with the jobs command to see if it’s still running.
When the backgrounded process finishes, you should see something like this:
[1]+ Done runme
Stopping a backgrounded process
If you want to stop a backgrounded process before you log off, you can bring it back to the foreground with an fg command and then press ^c (Ctrl + “c”) to kill it. Notice that, after doing this, the jobs command has no output:
$ jobs [1]+ Running sleep 1000 & $ fg 1 sleep 1000 ^C $ jobs $
Disowning a process
If you want to make sure a process running in the background will keep running even after you log off, you can use a disown command. In the command below, we start a process in the background and display it with the jobs -l command which includes the process ID (3926) rather than just the job number (1) in its output.
$ sleep 1234 & [1] 3926 $ jobs [1]+ Running sleep 1234 & $ jobs -l [1]+ 3926 Running sleep 1234 &
We then use the disown command to allow it to run outside of our terminal’s control. Note that the jobs command does not display any information on the disowned process.
$ disown 3926 $ jobs
When we log in again, the process might still be running – depending, in this case, on how many seconds of sleep we requested. We can look for it by looking for the process ID or command name:
$ ps -ef | grep sleep shs 3926 1 0 14:32 ? 00:00:00 sleep 1234 shs 4030 3982 0 14:36 pts/1 00:00:00 grep --color=auto sleep $ ps -ef | grep 3926 shs 4145 3982 0 14:55 pts/1 00:00:00 grep --color=auto 3926
Wrap-up
Running processes in the background can be convenient when you want to use your terminal window for something else while you’re waiting for the first task to complete. It just takes a little getting used to using &, ^z, bg, fg and the disown commands.
Source:: Network World