Is it possible to attach a terminal to an already running process by using its PID in a similar fashion to using the fg command for jobs?

  • like what?! what is the process you want to attach?!
    – user61928
    Sep 25, 2012 at 11:58
  • I would like to be able to view the output of a program that was started in another terminal
    – Tommy
    Sep 25, 2012 at 16:02

3 Answers 3


You can get that process's standard file descriptors, e.g. stdout:

tail -f /proc/<pid>/fd/1
  • 9
    I tried this solution with top and it doesn't seem to work. In a terminal I launched top and in another I used your command with the corred pid but got no output.
    – Tommy
    Sep 25, 2012 at 16:08
  • 2
    How do you do this on a mac?
    – Avner Barr
    Apr 30, 2017 at 20:46
  • what about stderr? I assume /proc/<pid>/fd/2, but then how do you get both stdout/stderr? May 27, 2017 at 8:07
  • 3
    What if this descriptor points to a socket? Tail can't read it. # ls -la /proc/24510/fd/1 lrwx------ 1 root root 64 Oct 31 08:34 /proc/24510/fd/1 -> socket:[444026]
    – Imaskar
    Oct 31, 2018 at 7:44
  • I had to cat /proc/<pid>/fd/1 . For some reason tail -f didn't work for me
    – forzagreen
    Jan 5 at 10:15

There are a few options here. One is to redirect the output of the command to a file, and then use tail to view new lines that are added to that file in real time.

Another option is to launch your program inside of screen, which is a sort-of text-based terminal application. Screen sessions can be attached and detached, but are nominally meant only to be used by the same user, so if you want to share them between users.

Else if you wish you can trace the process and see what it is doing with strace:

strace -e trace=open -p 22254 -s 80 -o output.txt
  • -p PID: Attach to the process with the process ID PID and begin tracing.
  • -s SIZE: Specify the maximum string size to print (the default is 32).
  • -o filename: Write the trace output to the file filename rather than to screen (stderr).
  • Finally it doesn`t seem to work. I launched top and in another I used your command with the corred pid but didn't get the expected output. When I use tail afterwards.
    – Tommy
    Sep 25, 2012 at 16:13
  • You need to use sudo with strace to attach to a process, and use -o ~/output.txt so the file is left in your home folder.
    – user76204
    Sep 25, 2012 at 16:18
  • Use the output file, dont send to screen! Dont be an idiot like I was and press Ctrl-C to get out of strace and then detached my PID. Scared the shit out of me on an important script. Jan 8, 2018 at 22:41

You can use these commands given below to attach a running process on another terminal:

  • retty (Man page: retty)
  • reptyr
  • screen


screen -S PID
retty $(pgrep PID)
reptyr PID
  • I was not able to find retty or reptry. Also, to be able to use screen I must launch the original process with it so it doesn't solve my issue.
    – Tommy
    Sep 25, 2012 at 16:06
  • you can install it using command sudo apt-get install retty but there are few limitations with retty. Check out the man page mentioned in answer.
    – devav2
    Sep 25, 2012 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.