Linux: Move Running Process to Screen

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.

Now, let’s see how to move a running process. Let”s first start a ping process on a terminal.

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 zsh and bash.

To reparent the process, run reptyr on the target screen terminal. 8217 is the PID of the process we got earlier from the jobs command.

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 and retty, and reptyr seems to be the better alternative – it has less problems and works on a wider range of platforms.

ibrahim = { interested_in(unix, linux, android, open_source, reverse_engineering); coding(c, shell, php, python, java, javascript, nodejs, react); plays_on(xbox, ps4); linux_desktop_user(true); }