Home Command line utilities How To Suspend A Process And Resume It Later In Linux
Suspend A Process And Resume It Later In Linux

How To Suspend A Process And Resume It Later In Linux

By sk
8835 Views

Let us say you run a program. But, you didn't know how long it will take to finish. The process keeps running several minutes. You don't want to 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. 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 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
Suspend A Process And Resume It Later In Linux Using Kill Command

Suspend A Process And Resume It Later In Linux

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>.

Does this 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.


Recommended read:


And, that's all. You know now how to pause and resume a process in Unix-like operating systems.

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like

4 comments

Paul Grunwald April 25, 2017 - 1:13 am

you should add the following:

: suspend the process
bg : run the suspended process in the background
fg : bring the background process to the forground

Reply
SK April 25, 2017 - 12:52 pm

Thank you. I will cover bg and fg commands in a separate guide soon.

Reply
Mallika May 17, 2020 - 12:09 am

>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?

[[email protected] see-initrd]£ kill -CONT 8745
[[email protected] 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:
[[email protected] 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:
[[email protected] see-initrd]£ kill -STOP 8745
[[email protected] 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
[[email protected] see-initrd]£

Reply
Thomas June 17, 2020 - 2:49 pm

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.

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More