49

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?

Melebius
  • 11,431
  • 9
  • 52
  • 78
Tommy
  • 633

3 Answers3

61

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

tail -f /proc/<pid>/fd/1
Jorge Castro
  • 71,754
  • 10
    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
  • 3
    How do you do this on a mac? – Avba Apr 30 '17 at 20:46
  • what about stderr? I assume /proc//fd/2, but then how do you get both stdout/stderr? – Alexander Mills May 27 '17 at 08: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 '18 at 07:44
  • I had to cat /proc/<pid>/fd/1 . For some reason tail -f didn't work for me – forzagreen Jan 05 '22 at 10:15
13

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).
Olorin
  • 3,488
  • 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. –  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 08 '18 at 22:41
4

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
Kulgar
  • 185
devav2
  • 36,312
  • 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