3

Given the PID of a process, is there a way to find out if this process has the stdin or stdout redirected?

I have one application that reads from the stdin. For convenience, I usually launch this application with stdin redirected from file, like this:

app < input1.txt

The problem is that sometimes I launch the application and forget what was the input file that I used. Is there a way to find out which file has been used to redirect the stdin?

Using ps -aux | grep PID allow me to see the command line used. But does not give me any information about the stdin or stdout.

I have also tried to look in top, as well as in /proc/PID/* but haven't found anything.

I am using CentOS 7, if that helps.

jww
  • 97,681
  • 90
  • 411
  • 885
rph
  • 901
  • 1
  • 10
  • 26
  • You should state a language. Possible duplicate of [Test stdout and stderr redirection in bash script](https://stackoverflow.com/q/9340129/608639), [Check if there is an stdout redirection in bash script](https://stackoverflow.com/q/26761627/608639), [Detect if stdout is redirected to a pipe (not to a file, character device, terminal, or socket)?](https://stackoverflow.com/q/23873078/608639), [How to tell if output of a command or shell script is stdout or stderr](https://unix.stackexchange.com/q/186449/56041), etc. – jww Nov 03 '18 at 22:44
  • @jww Those links are not useful. I am looking for a terminal solution. – rph Nov 05 '18 at 02:34
  • If you are just issuing commands then the question is probably off-topic here. This is one of those questions that has been asked and answered so many times you have to make an effort to avoid finding an answer. Bash, shell, terminal are nearly the same. Maybe try [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/). – jww Nov 05 '18 at 12:52

1 Answers1

6

You should just be able to look at /proc/<PID>/fd for this information. For example, if I redirect stdin for a command from a file:

sleep inf < somefile.txt

Then I fill find in the corresponding /proc directory:

$ ls -l /proc/12345/fd
lr-x------. 1 lars lars 64 Nov  4 21:43 0 -> /home/lars/somefile.txt
lrwx------. 1 lars lars 64 Nov  4 21:43 1 -> /dev/pts/5
lrwx------. 1 lars lars 64 Nov  4 21:43 2 -> /dev/pts/5

The same thing works when redirecting stdout to a file. If I run:

sleep inf > somefile.txt

Then I see:

$ ls -l /proc/23456/fd
lrwx------. 1 lars lars 64 Nov  4 21:45 0 -> /dev/pts/5
l-wx------. 1 lars lars 64 Nov  4 21:45 1 -> /home/lars/somefile.txt
lrwx------. 1 lars lars 64 Nov  4 21:45 2 -> /dev/pts/5
larsks
  • 277,717
  • 41
  • 399
  • 399