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.
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
or ps aux
command, and then pause it, finally resume it using kill
command.
Let us see an example. I am going to download Ubuntu 18.04 netboot image using wget
command:
$ wget http://archive.ubuntu.com/ubuntu/dists/xenial/main/installer-amd64/current/images/netboot/mini.iso &
Here, &
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:
$ ps
Sample output:
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:
$ ps
Sample output:
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 [1]+ Stopped wget http://archive.ubuntu.com/ubuntu/dists/xenial/main/installer-amd64/current/images/netboot/mini.iso
See? The 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 ps
command.
$ ps
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.
TL;DR
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.
Recommended read:
- 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
7 comments
you should add the following:
: suspend the process
bg : run the suspended process in the background
fg : bring the background process to the forground
Thank you. I will cover bg and fg commands in a separate guide soon.
>To verify it if the process is running, run ps command.
This doesn’t verify that it is running, since the Stopped message only shows up once, as far as I can tell.
I say this, because when I try to -CONT this STOPPED process,
it immediately reports that it got stopped, rt?
[root@centos7 see-initrd]£ kill -CONT 8745
[root@centos7 see-initrd]£ ps
PID TTY TIME CMD
7569 pts/0 00:00:00 sudo
7572 pts/0 00:00:00 bash
8745 pts/0 00:00:00 cpio
10529 pts/0 00:00:00 ps
[1]+ Stopped cpio –extract –no-absolute-filenames initramfs-3.10.0-957.27.2.el7.x86_64 (wd: /home/mallikab/see-initramfs)
(wd now: /home/mallikab/see-initrd)
Running ps now, even though it says nothing about Stopped, doesn’t indicate that the process is running:
[root@centos7 see-initrd]£ ps
PID TTY TIME CMD
7569 pts/0 00:00:00 sudo
7572 pts/0 00:00:00 bash
8745 pts/0 00:00:00 cpio
10568 pts/0 00:00:00 ps
You can confirm it wasn’t running, because issuing a -STOP on a stopped process, produces no ‘Stopped’ messaging:
[root@centos7 see-initrd]£ kill -STOP 8745
[root@centos7 see-initrd]£ ps
PID TTY TIME CMD
7569 pts/0 00:00:00 sudo
7572 pts/0 00:00:00 bash
8745 pts/0 00:00:00 cpio
10746 pts/0 00:00:00 ps
[root@centos7 see-initrd]£
This wont work if RAM is the issue.
You can check with $ top and sorting by memory (shift + m) that the memory usage of the stopped process will just be frozen.
So no memory will be freed this way.
But it may reduce swapping
You can verify the process is in a stopped state by using top and checking for a stopped process in the upper section. This doesn’t release memory from the process but it sure does help when other work is being hindered!
The memory is not freed but the program is put into swap memory for what I’ve heard.
Swap memory could be space on your hard drive