Picture this scenario. You run a program. But, you don't know how long it will take to finish. The process keeps running several minutes. You can't wait that much longer, because some other important programs are waiting in the queue. Have you ever been in a situation like this? No worries! I just found a simple trick to suspend a process and resume it later in Linux.
What I am going to do is just pause the currently running process, do some other important tasks, and then resume the stopped process after all other processes are completed.
This can be very useful when you have less RAM or Processor to do multi-task. You can pause the running processes at any time, and resume them later, without having to start them all over again.
Now let us go ahead and learn to suspend or pause a running process and resume it later in Linux and Unix-like operating systems.
Table of Contents
Suspend a process and resume it later in Linux
This is absolutely an easy! All you have to do is find the PID (Process ID) and using
ps aux command, and then pause it, finally resume it using
Let us see an example. I am going to download Ubuntu 18.04 netboot image using
$ wget http://archive.ubuntu.com/ubuntu/dists/xenial/main/installer-amd64/current/images/netboot/mini.iso &
& symbol will move the running task (i.e.
wget) to the background without closing it.
Now, I want to pause this task and run other important tasks. To do so, first find the running processes using command:
PID TTY TIME CMD 30056 pts/0 00:00:00 bash 16143 pts/0 00:00:01 wget 32677 pts/0 00:00:00 ps
As you see, the PID of wget command is 16143.
Let us stop this process now. To do so, run the following command from your Terminal:
$ kill -STOP 16143
Verify whether the process has been stopped or not using command:
PID TTY TIME CMD 8155 pts/0 00:00:00 bash 16143 pts/0 00:00:01 wget 16398 pts/0 00:00:00 ps + Stopped wget http://archive.ubuntu.com/ubuntu/dists/xenial/main/installer-amd64/current/images/netboot/mini.iso
wget process has been stopped.
Go ahead and do other important tasks. Once all tasks are completed, resume the stopped process using command:
$ kill -CONT 16143
To verify it if the process is running, run
See? The process which we stopped earlier has been resumed!
Like I said already, this will be helpful if you can't do multi-task in system that has low RAM or CPU speed.
First, find the pid of the running process using ps command. Then, pause it using
kill -STOP <PID>, and then hibernate your system. Resume your system and resume the stopped process using command
kill -CONT <PID>.
Will it work after restarting my system?
You might wonder, will this also work after a full system shutdown or reboot? NO. Because, the PIDs of the processes will automatically change after rebooting your system. They do not persist across reboot. In such cases, you can suspend or hibernate your entire system, and resume them when you are ready.
And, that's all. You know now how to pause and resume a process in Linux and Unix-like operating systems. Hope this helps.
- Auto Shutdown, Reboot, Suspend, Hibernate Your Linux System At A Specific Time
- Automatically Wake Up Your Linux System From Sleep Or Hibernation Mode
- Reptyr – Move A Running Process From One Terminal To Another Without Closing It
- How To Add Linux Commands To The Queue And Execute Them One By One
- How To Display Process Information Using Procs On Linux