Attaching a process to a terminal in LInux
This is not possible “cleanly”. There are tools such as screen and tmux that create a remanent virtual terminal: you can start your program inside screen, detach the screen session, and later reattach the screen session on a different terminal. But that requires planning ahead.
It is possible to use ptrace to fiddle with the process's file descriptors to reconnect them to another terminal. That doesn't work reliably because it might create inconsistencies in the process's data structures: some programs won't mind, others will crash. You can do this by attaching the program in a debugger and issue the right sequence of system calls (at least open
, dup
and close
for each of standard input, standard output and standard error). There are several existing tools that can do this, for example neercs and retty.
See also How can I pause up a running process over ssh, disown it, associate it to a new screen shell and unpause it?, View Script Over SSH?, Can I nohup/screen an already-started process?, Resume command running in dropped SSH session.
You can try GNU screen. It is made for this purpose.
Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells