1

I want to discover the PID of the producer process in a pipeline:

producer | consumer

I don't always control the producer process, I can request that they echo the PID from the producer like so:

log.info(JSON.stringify({pid: process.pid}) + '\n');

but is there any other way to discover what the PID of the producer might be?

If the pipeline is not a background process, then one clue would be that it's the most recent process created by the parent PID, which I do know.

  • 3
    Related: https://unix.stackexchange.com/a/405488/117549 – Jeff Schaller Sep 22 '19 at 01:48
  • doable via /proc/$pid/{0,1} as Jeff pointed the related question. privilege may be required – James Li Sep 22 '19 at 04:02
  • 1
    What's the use case for this? – Kusalananda Sep 22 '19 at 07:23
  • the consumer is reading from tty stdin, not the producer, so I have to send a SIGINT from consumer to producer in order to do a clean shutdown – Alexander Mills Sep 22 '19 at 08:00
  • 1
    Your producer should clean exit on SIGTERM and SIGPIPE too, not just on SIGINT. If it doesn't and it's not possible to fix it, you should start it from your consumer, or from a wrapper, in which case you could easily retrieve its pid. All the other "solutions" are non-portable dangerous hacks, nothing more. –  Sep 22 '19 at 12:36

0 Answers0