简体   繁体   中英

Other programs in pipe

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 - ie, 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.

If you're on Linux you can use the /proc filesystem to check how commands communicate over pipes. However, this is not really portable.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM