rph
rph

Reputation: 911

Check if a already running process has the stdin redirected on Linux?

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.

Upvotes: 3

Views: 1768

Answers (1)

larsks
larsks

Reputation: 311596

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

Upvotes: 6

Related Questions