Attach to a processes output for viewing

LinuxLoggingCommand Line-Interface

Linux Problem Overview


How would I 'attach' a console/terminal-view to an applications output so I can see what it may be saying?

How would I detach from an applications output without killing the application?

Normally if you fire up a talkative application using the command line you get to see all kinds of wonderful output. However, let’s say I have a particularly chatty programming running, like KINO, and I want to view its output at any given moment without restarting it through the command line. I cannot; at least I don't know how.

Linux Solutions


Solution 1 - Linux

I think I have a simpler solution here. Just look for a directory whose name corresponds to the PID you are looking for, under the pseudo-filesystem accessible under the /proc path. So if you have a program running, whose ID is 1199, cd into it:

$ cd /proc/1199

Then look for the fd directory underneath

$ cd fd

This fd directory hold the file-descriptors objects that your program is using (0: stdin, 1: stdout, 2: stderr) and just tail -f the one you need - in this case, stdout):

$ tail -f 1

Solution 2 - Linux

I was looking for this exact same thing and found that you can do:

strace -ewrite -p $PID

It's not exactly what you needed, but it's quite close.

I tried the redirecting output, but that didn't work for me. Maybe because the process was writing to a socket, I don't know.

Solution 3 - Linux

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, it's a big pain in the ass.

Solution 4 - Linux

For me, this worked:

  1. Login as the owner of the process (even root is denied permission)

     ~$ su - process_owner
    
  2. Tail the file descriptor as mentioned in many other answers.

     ~$ tail -f /proc/<process-id>/fd/1 # (0: stdin, 1: stdout, 2: stderr)
    

Solution 5 - Linux

You can use reptyr:

sudo apt install reptyr
reptyr pid

Solution 6 - Linux

> How would I 'attach' a > console/terminal-view to an > applications output so I can see what > it may be saying?

About this question, I know it is possible to catch the output, even when you didn't launch sceen command before launching the processus.

While I never tried it, I've found an interesting article which explains how to do using GDB (and without restarting your process).

[redirecting-output-from-a-running-process][1]

Basically:

  1. Check the open files list for your process, thanks to /proc/xxx/fd
  2. Attach your process with GDB
  3. While it is paused, close the file you are interested in, calling close() function (you can any function of your process in GDB. I suspect you need debug symbols in your process..)
  4. Open the a new file calling the create() or open() function. (Have a look in comments at the end, you'll see people suggest to use dup2() to ensure the same handle will be in use)
  5. Detach the process and let in run.

By the way, if you are running a linux OS on i386 box, comments are talking about a better tool to redirect output to a new console : 'retty' . If so, consider its use.

[1]: http://etbe.coker.com.au/2008/02/27/redirecting-output-from-a-running-process/ "redirecting-output-from-a-running-process"

Solution 7 - Linux

I wanted to remotely watch a yum upgrade process that had been run locally, so while there were probably more efficient ways to do this, here's what I did:

watch cat /dev/vcsa1

Obviously you'd want to use vcsa2, vcsa3, etc., depending on which terminal was being used.

So long as my terminal window was of the same width as the terminal that the command was being run on, I could see a snapshot of their current output every two seconds. The other commands recommended elsewhere did not work particularly well for my situation, but that one did the trick.

Solution 8 - Linux

An even simpler option is cat:

cat /proc/<pid>/fd/1

Especially if tail does not work for you due to permission errors (even using sudo), or if the process to monitor display a progress bar using curses or an equivalent library.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionaggitanView Question on Stackoverflow
Solution 1 - LinuxlicornaView Answer on Stackoverflow
Solution 2 - LinuxPaul ScheltemaView Answer on Stackoverflow
Solution 3 - LinuxDon WerveView Answer on Stackoverflow
Solution 4 - LinuxakkiView Answer on Stackoverflow
Solution 5 - LinuxZhigang ZhangView Answer on Stackoverflow
Solution 6 - Linuxyves BaumesView Answer on Stackoverflow
Solution 7 - LinuxRedScourgeView Answer on Stackoverflow
Solution 8 - LinuxcaramView Answer on Stackoverflow