When working on Linux, you fire up one command after another. For example, you edit a configuration file using the nano or vim editors or download files using the wget
command and suddenly want to take a look at some other files or need to execute another command.
In this scenario, you have two options. First, close the configuration file or cancel copying files, but it can cost you time and effort if you are in the middle of them. Another solution is to place that process in the background, or, in other words, suspend it for a while and resume it later.
Suspend a Process in Linux
In Linux, you can use the CTRL+Z shortcut key to suspend any command in the background. This works for Ubuntu and all other Linux distributions.
Then you can continue with your other important task, and once you are done, you can resume it later.
Take a scenario: I am editing a text file using Nano and suddenly want to suspend it in the middle of editing. I will use the CTRL+Z shortcut key to suspend it, as shown below.
You can place many processes in the background depending upon the size of your system memory, and once you are done, you can find them using the ps
or jobs
command and resume them later.
List Suspended Processes in Linux
To list all of the suspended processes in the background, you can use two different commands: ps
and jobs
(recommended).
What is the difference between the ps and jobs commands?
ps
command-list all of the running processes in your system. While the jobs
command only lists the suspend process suspended using the CTRL+Z shortcut key in your Linux system.
Let us list all of the suspended processes using the ps
command in Linux.
- ps
Below is the behavior of this command.
If you look carefully, you can spot two more processes running in the background that got listed. The bash process refers to the current running bash shell within your terminal, while the ps process is used to list all these processes.
Now let us use the jobs
command (recommended) to list all the suspended processes in the background.
- jobs
Below is the behavior of this command.
You can clearly see, in this case, only the suspended process listed by the jobs
command.
Resume the Suspended Process
Once you are done with your important work, you can resume the suspended process using the fg
command.
If you execute the fg
command without a process ID, it will resume the latest process currently in the background.
For example, if you have two or three different processes in the background, the fg
command alone resumes the recently suspended process in the background with the (+) symbol, as shown below.
- fg
Below is the behavior of this command.
Above, you can spot the (+) symbol along with the process ID when the command is executed. In my case, nano file.txt
is with the (+) symbol. So, it will be the first process to resume using the fg
command.
You can resume any specific process instead of the recently suspended one. In that case, you need to specify the process ID of that particular process along with the fg
command to resume it in the foreground.
- fg <suspended-process-id>
Replace <suspended-process-id>
with the suspended process ID you want to resume. In my case, I will replace <suspended-process-id>
with 2
to resume my ping command.
Will it work after restarting my system?
No, they do not persist across reboots. The suspended process gets closed after you reboot your entire system due to PID changes. In such cases, hibernate your entire system instead of suspending each process.
Keep Suspended Processes Running in the Background
The bg
command is beneficial if you want to keep running your process in the background without being frozen.
What is the difference between bg and fg in Linux?
The fg
command switches the recently suspended process from background to foreground, while the bg
command keeps the process running in the background.
Below, I started the sleep 500
command and suspended it using the CTRL+Z shortcut key, which froze the process. Then again, I used the bg
command to relaunch the suspended process in the background.
- jobs
- sleep 500
- jobs
- bg $1
- jobs
If you have multiple suspended processes, then specify the specific process ID with %
to keep it running in the background.
Stop the Suspended Process
If you do not require a suspended process anymore, you can directly stop them with their PID in the background using the kill command.
📝 Note
We strongly recommend opening each process one by one and shutting it down by its own method to close it gently.
To do so, you first need to find out the PID of the suspended process using the ps
command. Then specify that PID with the kill
command to stop that process.
- ps
- kill -9 <process-id>
Replace the <process-id>
with the process you want to kill or stop. In my case, I want to stop the top
background process, so I will replace <process-id>
with the top
PID, as shown below.
Instead of the specific process, you can use the below command if you want to stop or kill all of the suspended processes at once.
- kill -9 $(jobs -p)
Below is the behavior of this command.
Final Thought
I frequently suspend my processes and run them in the background when attention is not necessary. This includes fetching large files using the wget
command, downloading projects using git clone
, or uncompressing compressed files.