How to pause a running processes and resume after some time in Linux or Unix ?

Today we will check how to pause a running task on linux so that we can resume it after some time.

Suppose you ran a task to download multiple packages, however there is an activity of network restart and you want to pause the download so that it doesn’t get killed/interrupted itself.

Hence we can pause this task and after the activity gets over, we can resume the task again.

Here is an example:- I am started a job to download all my system packages, now we will see how to interrupt this task and resume again.

I. Stopping and resuming current jobs
In this section, we will see how to stop and resume currently running jobs in current shell.

1. Started the Job

[root@nglinux pkg2]# rpm -qa | xargs yumdownloader
bison-2.4.1-5.el6.i686.rpm                                                                  | 626 kB     00:04     
nss-sysinit-3.28.4-4.el6_9.i686.rpm                                                         |  51 kB     00:00     
avr-gcc-c++-4.5.0-2.el6.i686.rpm                                                            | 3.0 MB     00:08     
xfce4-notes-plugin-1.7.7-1.el6.i686.rpm                                                     | 146 kB     00:00     
texlive-texmf-2007-39.el6_7.noarch.rpm                                                      | 2.8 MB     00:11     

2.Now stopping this job by pressing Ctrl+Z

squirrel-libs-2.2.4-2.el6.i686.rpm                                                          | 131 kB     00:00     
perl-Text-Unidecode-0.04-7.1.el6.noarch.rpm                                                 | 115 kB     00:00     
^Zpertux-0.3.4-1.el6.i686.rpm              94% [===============================  ] 394 kB/s |  61 MB     00:09 ETA 
[1]+  Stopped                 rpm -qa | xargs yumdownloader

Are you thinking you killed the job ? No thats wrong, you just stopped or paused it…
P.S:- Killing was done by Ctrl+C, not by Ctrl+Z or knife 😛

3.Now check the stopped jobs.

[root@nglinux pkg2]# jobs
[1]+  Stopped                 rpm -qa | xargs yumdownloader
[root@nglinux pkg2]# 

We have only one job with stopped/paused state.

4. Now resume this job by running via JOB ID with fg(to bring the job to foreground or on your screen).

[root@nglinux pkg2]# fg %1
rpm -qa | xargs yumdownloader
supertux-0.3.4-1.el6.i686.rpm                                                               |  65 MB     05:03     
texlive-dvips-2007-60.el6_7.i686.rpm                                                        | 185 kB     00:02     

So its quite easy to stop and then resume the jobs in Linux.

However there is a concern here, if you logout from your current session, or the parent process of above process gets killed, it will also be flushed from memory.

So for such processes, we need to make sure to start the process in background to keep it alive.

Trick to keep the process running

[root@nglinux ~]# rpm -qa | xargs yumdownloader &
[1] 20836
[root@nglinux ~]#

Now you will say how to pause it ? and how to resume it in future ?

So the professional way of doing so is to use Kill arguments.

II. Stopping and Resuming background Jobs or processes using kill command

To do this, we can use Kill command signal SIGSTOP to change the process state from running to stop.

[root@nglinux kickstart_cd]# kill -SIGSTOP 20836 20837

Now if you look at the process state, it is changed to stop state i.e. T.

[root@nglinux kickstart_cd]# ps aux | grep -i yum | more
root     20836  0.0  0.0   4344   564 pts/1    T    00:12   0:00 xargs yumdownloader
root     20837 51.7  7.0 120368 72840 pts/1    T    00:12   0:37 /usr/bin/python /usr/bin/yumdownloader

Now suppose after the activity maybe after few hours, you can continue the stopped jobs using below command.

[root@nglinux kickstart_cd]# kill -SIGCONT 20836 20837

We can verify the same, using the ps command. Now the state is changed to sleeping and running for child process.

[root@nglinux kickstart_cd]# ps aux | grep -i yum | more
root     20836  0.0  0.0   4344   564 pts/1    S    00:12   0:00 xargs yumdownloader
root     20837 32.6  7.0 120368 72840 pts/1    R    00:12   0:40 /usr/bin/python /usr/bin/yumdownloader 

So from now onwards use the interesting capability of Linux system to stop the processes and then continue further.

I hope you liked the article. Subscribe to this blog to stay updated.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments