Objective: Move/detach/reparent a running process to a new screen terminal.
Sometimes you start a job on the terminal, and realise later that it is taking much longer to complete than expected. If you exit the terminal, the process will be killed. To overcome this situation, the process has to be moved or detached from the terminal and there are a few ways to do it.
The first way (and my personal favourite) is to use the
reptyr utility. If your Linux distribution does not have it installed, install the
reptyr package. Below are commands to install the package on Debian and RedHat based distributions.
# apt-get install reptyr
# yum install reptyr
Now, let’s see how to move a running process. Let”s first start a
ping process on a terminal.
t1# ping -q router01 PING router01.internal.lan (192.168.10.1) 56(84) bytes of data. ^Z + Stopped ping -q router01 t1# bg 1 + ping -q router01 & t1# jobs -l + 8217 Running ping -q router01 & t1# disown
After starting the
ping process, we suspend the process using
Ctrl-Z, run it as a background job, get the PID of the process using
jobs and run
disown – this will prevent the shell from sending
SIGHUP to this process when the shell exits. The
disown is a shell builtin command available on
To reparent the process, run
reptyr on the target screen terminal. 8217 is the PID of the process we got earlier from the
t2# screen t2# reptyr 8217 ^C --- router01.internal.lan ping statistics --- 19 packets transmitted, 19 received, 0% packet loss, time 20731ms rtt min/avg/max/mdev = 0.816/1.072/1.341/0.175 ms
After the process is moved and when you press
Ctrl-C on the new terminal, the ping process will terminate and display the summary statistics.
reptyr is available on Linux 32-bit, Linux 64-bit and ARM platforms.
The second way is to use
retty – a similar tool as
reptyr which does exactly the same. However,
retty only works on 32-bit versions of Linux.
I have personally used both
reptyr seems to be the better alternative – it has less problems and works on a wider range of platforms.