By drupalmaster, 23 April, 2024

In this article, I want to explain the importance of being able to foreground and background a process running in Linux. If you’ve never used this technique or have never heard of it, then you will be happy to learn that it exists on all Linux distros and making use of it can really enhance your productivity and workflow.

Many people when working in the Linux terminal will open more than one window so they can multi-task. And, that’s a good thing. Others will open up new Linux terminal windows using tabs instead of actual full Linux terminals, and that’s a great thing as well. But, if you weren’t aware of another means whereby you can be working on one task, then if interrupted and need to leave that job and return to it later, or if you wish to start up a new task without closing the previous one, then learning how to background and foreground a process is something that you will want to learn.

So, what does it mean exactly to background a process? Let’s look at a simple example of how this works. Let’s suppose that I’m in my terminal in FerenOS Linux–could be Debian, Arch, CentOS, Red Hat, doesn’t matter–and I’ve launched htop. After monitoring running processes in my system for a few minutes, I get a phone call or I’m interrupted in some other way and decide I need to step away from the computer for a few moments to take the call or address the issue and return later to finish what I was doing. But, I don’t want to close my existing htop session since I’m actively monitoring the performance of my machine. Well, I don’t have to close the terminal or shutdown the PC. What I can do instead is send the current task/job of running the htop process to the background where it will be stopped (actively) on the screen, but remain running in the background in memory. This preserves the active state of htop so that I don’t have to restart it later. So, how do I do this?

After starting htop, I can send the job to the background to get it off the active terminal screen by holding down the Ctrl key on the keyboard and pressing Z. This is a keystroke combination of Ctrl + Z. Unlike Ctrl +C which stops the process/task/job running in the terminal, Ctrl + Z halts the job by sending it to the background where it can continue running out of sight. So, to background the running process of htop in my terminal, on my keyboard, I press:

Ctrl + Z

and the resulting display on my terminal screen will be shown as in the example below:

datapioneer@ferenOS-MainPC:~$ htop

[1]+   Stopped    htop

Here, my username is datapioneer and my hostname on the PC is ferenOS-MainPC. I was running htop, but when I pressed Ctrl + Z on my keyboard, I was returned to the main terminal window with htop showing up on the top line and the other information that you see above on the second line. This status information indicates that I have one job (as noted with the [1]+ to the left, and this job has been Stopped on the active terminal window, but remains running in the background. The job which has been effectively back-grounded is htop because that is what is showing up to the far right on the 2nd line. The number “[1]” in brackets indicates that I have one job which has been back-grounded and that job is job # 1.

Now, I can go on to perform other jobs and when I’m ready to return to htop once again, I can simply recall this job to the foreground so that it becomes active once more. The way to do this is to type:

$ fg

in the terminal, and since I have only one job which has been back-grounded, the last job will be brought to the fg (foreground) in the terminal and resume where I left off. Performing this in the terminal will result in the following display:


which returns me to my previous htop session.

Now, let’s look at another example but this time I am going to have two jobs running instead of one. Let’s assume that I start an htop process, then use Ctrl + Z to send that process to the background, then start up glances (another system monitoring utility), and then send that process to the background as well using Ctrl + Z in the terminal to background that process. After performing this action, the resulting terminal screen status will look like this:

stopped processes

Now, you can clearly see that I have two processes: htop and glances, both of which have been sent to the background and are indicated as Stopped in the foreground. The listing of these processes is shown to the right. Job [1]+ indicates the htop process and job [2]+ shows up as glances. But, if I clear my terminal window to go on and do other things and I can’t remember how many jobs I have back-grounded, how do I retrieve this information so I can bring one or more of them back to the foreground to continue where I left off? This is simple as well. To look at the jobs that have been sent to the background, simply type:
datapioneer@ferenOS-MainPC:~$ jobs

[1]-   Stopped      htop
[2]+  Stopped     glances

and the above will be displayed in the terminal screen. This indicates two stopped jobs: [1]- indicating htop as the first job which was sent to the background and [2]+ as the second job most recently sent to the background. If I wish to retrieve glances, then all I need to do is type fg and the most recently stopped job sent to the background; i.e., glances, will be returned to the foreground and resume. However, let’s assume that I want to resume htop instead. To do this, all I need to do is type:

$ fg 1\

fg followed by the number of the job I want returned to the foreground, which in this case is job # 1, and hit Enter. Htop will be resumed in the foreground and job # 2, which is glances will remain in the background as a running process. Now, if I quit this job by pressing F10 and hit Enter to Send F10 to htop, that process will be stopped in the foreground as well. If I want to see the status of the remaining jobs running in the background, I simply type: 

datapioneer@ferenOS-MainPC:~$ jobs
[2]+ Stopped    glances

once again and I will see that glances (job # 2) is still running in the background. I can recover that job now by simply typing:

$ fg

since it is the last job or only job in this case which is in the background, and glances will once again be returned to the foreground as an active job.

One final note on back-grounding and foregrounding jobs in Linux. If you want to start up a job and immediately send it to the background as a stopped process, you can accomplish this as well. For example, if I want to start up htop as a background process by immediately sending it to the background, I can accomplish this by typing:

datapioneer@ferenOS-MainPC:~$ htop & 
[1] 25942

and the process htop will be started since I used the “&” symbol after it and the process ID (PID) of 25942 indicates that job # 1 has been sent to the background. If I hit the Enter key to start the next command in the Terminal, the following status will result:

[1]+     Stopped      htop

letting me know that I have a stopped process called htop which is still running in the background. Then, when I want to return to htop once again, all I need to do is type fg and press Enter, and htop will resume as a foreground process once more.

htop resumed