15

If I start an app with this command:

/path/to/my/command >> /var/log/command.log

And the command doesn't return, is there a way, from another prompt, to see what the STDOUT redirect is set to?

I'm looking for something like either

cat /proc/PID/redirects

or

ps -??? | grep PID

but any method will do.

21

Check out the fildescriptor #1 (stdout) in /proc/$PID/fd/. The kernel represents this file as symbolic link to a file the descriptor is redirected to.

$ readlink -f /proc/20361/fd/1
/tmp/file
  • Perfect! Thanks! Can you provide a link or details about the other redirects such as STDERR, ie which numbers they are? – Rich Jun 27 '11 at 13:30
  • At least on Linux (and I believe on every other known UNIX-like OS) they are 0: stdin, 1: stdout, 2: stderr (standard error). Note there are also C macros defined in <stdio.h>: STD{IN,OUT,ERR}_FILENO. See 'man stdout' for details. – Petr Uzel Jun 27 '11 at 13:35
  • 0, 1, 2 for stdin, stdout and stderr respectively are guaranteed in all Unix/Unix-like systems, and are also true on Windows. – Paul Stelian Nov 16 '16 at 18:16
  • on MacOS any way to do this? the /proc/<pid>/fd/1 does not exist, maybe I can use mkfifo? – Alexander Mills Dec 22 '18 at 6:19
4

A useful tool to see what files are opened by what processes is lsof. You can point it at a specific process with lsof -p1234, and you'll see mostly the same information as you can get with ls -l /proc/1234/fd under Linux, i.e. what files are opened.

The most useful thing with lsof is going the other way round: lsof /path/to/file tells you what processes are using that file.

0

A lot of answers mention doing it this way:

tail -f /proc/{PID}/fd/1

However, I've found that it doesn't always work. Alternatively, the cat sometimes yields results.

cat /proc/{PID}/fd/1

where {PID} can be looked up using the ps aux command.

Also good to mention, is that the number on the end of the command (in this case fd/1) can be changed for other outputs.

 /proc/{PID}/fd/0 # STDIN
 /proc/{PID}/fd/1 # STDOUT
 /proc/{PID}/fd/2 # STDERR

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.