Every Linux user knows the following scenario: a running process should be moved to another terminal. An example: an application update takes longer than expected and the status should be monitored on the go in GNU Screen. It’s just nasty, if you forgot to prepare this prior to that.
Happily, there are plenty tools for this scenario. A couple of years ago, I found the tool retty – unfortunately it was not very stable and therefore migrated applications often crashed. I was more successful with the utility reptyr, which is available for the most Linux distributions (under Enterprise Linux you get the tool via the EPEL repository).
Before a process can be moved to a GNU screen session, it needs to be executed in background by pressing CTRL+Z. After executing bg, the process is running in background.
$ program CTRL+Z $ bg
The next step is to detach the process from the current terminal in order to attach it to the new one. To be more precise, the current job is removed from the table of active jobs – using the disown command:
$ disown %1
Afterwards, GNU Screen is started and the application’s PID is gathered – e.g. using ps or pgrep.
$ screen $ pgrep program
Finally, the PID is passed to reptyr to migrate the process to the new terminal:
$ reptyr PID