Is it possible to attach a terminal to an already runing process by using it's 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 '12 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 '12 at 16:02

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

tail -f /proc/<pid>/fd/1
  • 7
    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 '12 at 16:08
  • How do you do this on a mac? – Avner Barr Apr 30 '17 at 20:46
  • what about stderr? I assume /proc/<pid>/fd/2, but then how do you get both stdout/stderr? – Alexander Mills May 27 '17 at 8:07

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

  • This solution works well for me. Thank you. – Tommy Sep 25 '12 at 16:05
  • 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 '12 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 '12 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. – Weston Ganger Jan 8 at 22:41

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

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

Example:

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 '12 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 '12 at 17:36

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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