Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

migrated from Dec 13 '10 at 7:12

This question came from our site for professional and enthusiast programmers.

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

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.

share|improve this answer

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

Redirect STDERR / STDOUT of a process AFTER it's been started, using command line?

share|improve this answer

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

share|improve this answer
Sounds interesting, but can it attach to something that wasn't originally started in one of its sessions? – Chris Stratton Dec 13 '10 at 6:38

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.

share|improve this answer

You must log in to answer this question.

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