I have a file opened in vi
running in a terminal(xterm
), if I directly closes the terminal without first closing the file then I can see the vi
still running in the background(ps x
). Now is there any way to attach that process i.e. vi to some other terminal so that I can continue my work on the file. I have also tried fd
command but it fails.
4 Answers
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
-
1Sounds interesting, but can it attach to something that wasn't originally started in one of its sessions?– Chris StrattonDec 13, 2010 at 6:38
You can use GDB to attach a running process to a terminal.
Here's a description spread across several answers to a question (particularly see the one by Mirek):
No way to do that. Just kill the detached process. If you want to do this intentionally, have a look at the 'screen' command. This will not save you if your underlying shell dies though.
vi &
detachs the process from the existing terminal