Reputation: 2857
If a program PROG
is invoked with pipes,
progA | progB | PROG | progC ...
Is there a way for it to tell in what context it was invoked - i.e., from/to what other programs (A, B, C...) it is receiving or sending piped output?
I'm mostly interested in the immediate predecessor to PROG
(in the example above, progB
), but am also curious about the more general question.
Upvotes: 2
Views: 138
Reputation: 212268
You can use ps
to show all the processes which have the same parent. For example, if PROG has pid PID, on Linux you can do:
ps --ppid $(ps -o ppid= $PID)
to get a listing of all the commands in the pipeline. (Actually, you'll get all the commands that are children of the shell that invoked the pipeline, which may be sufficient. You can check the process group of each to determine which ones are actually in the pipeline.) The order in which they print is not necessarily the order they appear in the pipe, put you can look in /proc/pid/fd
to see the inode of each process' inputs to determine how they line up.
Upvotes: 2
Reputation: 58828
If you're on Linux you can use the /proc
filesystem to check how commands communicate over pipes. However, this is not really portable.
Upvotes: 2