Suspending a process in Linux is a simple task. But sometimes, the processes running in the Terminal take longer than expected, and it is impossible to wait. Hence, it is essential to learn how to suspend a process in Linux.
The most efficient solution is to pause or suspend the running process, perform some other required operations and then resume the suspended process.
Process Suspension
Process suspension refers to a process that has been switched off from the running state to the paused state. Although the process exists in the ready queue, it is not scheduled for execution.
Suspend a Process in Linux
System administrators need to suspend a process for a variety of reasons. For instance, there could be many interactive requests coming in for processing, or maybe there is a high-priority process that needs execution first. There are multiple ways to pause and resume the process using the kill command, shortcut keys, and signals. We will look at all the methods in detail.
1. Suspend a Process in Linux Using the Kill Command
Before we suspend a process, we need to find the Process ID (PID) of the process which is currently running. Let’s run a process first. Here I am running a wget
command to download Ubuntu distribution:
sudo wget http://archive.ubuntu.com/ubuntu/dists/xenial/main/installer-amd64/current/images/netboot/mini.iso &
Make sure to add the & so that the task runs in the background without interruption. Now, use the ps
command to view its Process ID (PID). For example, type:
ps
The output would look something like this:
You will get a list of processes currently running in your system. In this example, the PID of the wget process is 3781. To stop this process, run the kill command followed by the option -STOP
and PID. The syntax is:
kill option PID
Specifically, type:
kill -STOP 3781
To verify whether the process has been suspended, use the ps
command again. For instance:
ps
From the output, we can see that the process is no longer running. It is stopped by the shell.
Now, to resume the suspended process, use the kill
command again, followed by the -CONT
option. For example:
kill -CONT 3781
Again verify it using the ps command as shown below:
ps
The output would look like this:
You will see that the process is running now.
2. Suspend a Process in Linux Using the Ctrl + Z Shortcut key
Another method is to use the “Ctrl + Z” shortcut key in the Terminal. Let’s try this with the same wget
command.
sudo wget http://archive.ubuntu.com/ubuntu/dists/xenial/main/installer-amd64/current/images/netboot/mini.iso
Run the ps command to get the PID as shown below:
You will notice that the PID is different this time. This happens because Linux Kernel assigns a different PID each time a process starts. Now, stop the process using the “Ctrl + Z” shortcut key.
You will get a similar output:
The shell will inform you that the process has been suspended, and it will assign the suspended job a job ID. to verify, use the ps
command. For example:
ps
The output would look something like this:
To resume it, type:
fg
This will run the process in the foreground as shown in the output given below:
Alternatively, you can use the bg command to run it in the background. For instance, type:
bg
The difference between the shortcut key “Ctrl + Z” and the Kill command is that the Kill command is useful if the process is not attached to the Terminal and is running in the background.
3. Suspend a Process in Linux Using the SIGSTOP Signal
The third method to pause a process in Linux is to use the SIGSTOP
and SIGCONT
signal. Firstly, run a process using the following command;
sudo wget http://archive.ubuntu.com/ubuntu/dists/xenial/main/installer-amd64/current/images/netboot/mini.iso
Secondly, get the PID of the process using the ps
command. For example:
ps
Output:
Now, use the SIGSTOP
signal to stop the running process.
Verify it using the ps command once again and resume it with the SIGCONT
signal. Verify it once again using the ps command.
Resume a Process After Rebooting the Machine
You must be wondering if the suspended process will resume after you have powered off the system. The answer is NO, because the PIDs of the processes change after the reboot and are not persistent. Hence, you cannot suspend the process after rebooting the system. The best option is to hibernate your system if there is a suspended job that you wish to resume.
Other Process Related Useful Commands
To display the process identification number of the job and view the count, use the count command. Type:
count &
To check the status of the jobs, type:
jobs
If you have more than one job suspended in the background, enter:
fg %#
In this article, we have covered how to suspend and resume processes in three different ways: the keyboard shortcut, using the signal, and through the Terminal. However, these techniques will not work if you reboot your system as the process IDs will automatically change. To find out more about the process handling, check out this page. We hope this article was helpful to you.
If this guide helped you, please share it.