29

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.

3 Answers 3

36

Check out the file descriptor #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
4
  • Perfect! Thanks! Can you provide a link or details about the other redirects such as STDERR, ie which numbers they are?
    – Rich
    Jun 27, 2011 at 13:30
  • 1
    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, 2011 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. Nov 16, 2016 at 18:16
  • 2
    on MacOS any way to do this? the /proc/<pid>/fd/1 does not exist, maybe I can use mkfifo? Dec 22, 2018 at 6:19
12

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
9

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .