7

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.

3
  • vi & detachs the process from the existing terminal Dec 13, 2010 at 6:02
  • what if due to some other reasons my terminal get killed?
    – Ravi Gupta
    Dec 13, 2010 at 6:05
  • @Ravi it would be in a different process. So it doesn't matter. Dec 13, 2010 at 6:06

4 Answers 4

3

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.

1

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

1
  • 1
    Sounds interesting, but can it attach to something that wasn't originally started in one of its sessions?
    – Chris Stratton
    Dec 13, 2010 at 6:38
0

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):

https://stackoverflow.com/questions/593724/redirect-stderr-stdout-of-a-process-after-its-been-started-using-command-line

-1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .