Try   HackMD

Linux - pause and resume a process with signal

tags: linux

Example to pause a file copy process and resume it.

run a rsync with bandwidth limit to 1MB to copy file
[12:39:26] ycheng@nuc8:~/Downloads $ rsync --bwlimit=1000 -P ./ubuntu-22.04-live-server-amd64.iso /mnt/data/
ubuntu-22.04-live-server-amd64.iso
      3,178,496   0% 1000.98kB/s    0:24:22 
get main pid of the rsync process
[15:01:56] ycheng@nuc8:~ $ ps -elf | grep rsync
0 S ycheng    6111 13386  0  80   0 -  3883 poll_s 10:25 pts/0    00:00:00 rsync --bwlimit=1000 -P ./ubuntu-22.04-live-server-amd64.iso /mnt/data/
1 S ycheng    6112  6111  0  80   0 -  3811 poll_s 10:25 pts/0    00:00:00 rsync --bwlimit=1000 -P ./ubuntu-22.04-live-server-amd64.iso /mnt/data/
1 S ycheng    6113  6112  0  80   0 -  3876 poll_s 10:25 pts/0    00:00:00 rsync --bwlimit=1000 -P ./ubuntu-22.04-live-server-amd64.iso /mnt/data/
0 S ycheng    6475 26401  0  80   0 -  3692 pipe_w 10:26 pts/14   00:00:00 grep --color=auto rsync

Here there are 3 process threads to the rsync, the 6111 is parent process.

sent SIGSTOP signal to the process
[15:01:56] ycheng@nuc8:~ $ kill -STOP 6111
the rsync process "Stopped" and move to background.
[12:39:26] ycheng@nuc8:~/Downloads $ rsync --bwlimit=1000 -P ./ubuntu-22.04-live-server-amd64.iso /mnt/data/
ubuntu-22.04-live-server-amd64.iso
    119,570,432   8% 1000.49kB/s    0:22:26  
[1]+  Stopped                 rsync --bwlimit=1000 -P ./ubuntu-22.04-live-server-amd64.iso /mnt/data/
sent SIG
[15:01:56] ycheng@nuc8:~ $ kill -SIGCONT 6111
bring the rsync progress back to foreground
[12:39:26] ycheng@nuc8:~/Downloads $ fg
rsync --bwlimit=1000 -P ./ubuntu-22.04-live-server-amd64.iso /mnt/data/
    152,895,488  10% 1000.49kB/s    0:21:53  

Notes

  • you could also use "ctrl" + "z" to stop process if you have terminal running the process.
  • use "jobs" command to list process in background
  • could use "fg" command to continue

Reference

https://unix.stackexchange.com/questions/502065/is-there-a-way-to-pause-a-running-process-on-linux-systems-and-resume-later