How can I disown a running process and associate it to a new screen shell?

You can revoke “ownership” of the program from the shell with the disown built-in:

# press Ctrl+Z to suspend the program
bg
disown

However this only tells the shell not to send a SIGHUP signal to the program when the shell exits. The program will retain any connection it has with the terminal, usually as standard input, output and error streams. There is no way to reattach those to another terminal. (Screen works by emulating a terminal for each window, so the programs are attached to the screen window.)


It is possible to reattach the filedescriptors to a different file by attaching the program in a debugger (i.e. using ptrace) and making it call open, dup and close. There are a few tools that do this; this is a tricky process, and sometimes they will crash the process instead. The possibilities include (links collected from answers to How can I disown a running process and associate it to a new screen shell? and Can I nohup/screen an already-started process?):

  • grab (and the more ambitious cryopid)
  • neercs
  • reredirect
  • reptyr
  • retty

Using GNU screen is your best bet.

Start screen running when you first login - I run screen -D -R, run your command, and either disconnect or suspend it with CTRL-Z and then disconnect from screen by pressing CTRL-A then D.

When you login to the machine again, reconnect by running screen -D -R. You will be in the same shell as before. You can run jobs to see the suspended process if you did so, and run %1 (or the respective job #) to foreground it again.


To move a process between terminals or to reattach a disowned, you can use e.g. reptyr.