Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question
    
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
add comment

3 Answers 3

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

tail -f /proc/<pid>/fd/1
share|improve this answer
1  
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
add comment

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

share|improve this answer
    
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
add comment

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
share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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