There's been many times where I've accidentally started a long-running process in a terminal only to realize that I need to leave but can't disconnect or the process will die. If I were thinking I would have run the process in screen to begin with, but all too often I forget that step.

Well, I've found a solution that in a tool called reptyr that will re-attach the running process from one terminal to another without stopping the process. Getting started with using reptyr is actually pretty simple.

Installation

reptyr is available on CentOS/RedHat via the EPEL repository going back to CentOS/RedHat version 5 at least. I'm sure the package is also available in other distributions but I'm going to focus on CentOS here because that's what I use.

Install the EPEL repository:

sudo yum install -y epel-release

Install reptyr:

sudo yum install -y reptyr

Usage

Once you have a long-running process running in one terminal, log in with a different terminal. In the second terminal run screen. Running a ps ax will show you the running processes along with their process IDs (PIDs). From within screen attach the long-running process simply by passing the PID of the process:

reptyr <PID>

You will see the process continue in your screen session while in the other terminal you'll see the the process has been stopped with a message similar to the following:

[1]+   Stopped      <process command>

That's it! You've now transferred that process into a screen session that you can leave running while you go do your business.