Skip to main content
edited tags
Link
Gilles 'SO- stop being evil'
  • 865.5k
  • 205
  • 1.8k
  • 2.3k
updated for more general question.
Source Link
muru
  • 78.1k
  • 16
  • 213
  • 319

Get PID of lastany command in backgrounded piped sequence of commands

If, in bash, I execute:

cmd1 | cmd2 | cmd3... | cmdi | ... | cmdn &

where the cmd{1..n} may not be distinct, how do I get the PID of cmdncmdi? Alternatively, how can I signal the cmdncmdi process? (For example, send it SIGUSR1?) pkill/pgrep, pidof etc. don't look like good answers, since other instances of cmdncmdi maybe running, including as part of the same pipeline. jobs -p gives the PID of cmd1, for me.

i can be anything in {1..n}.

Get PID of last command in backgrounded piped sequence of commands

If, in bash, I execute:

cmd1 | cmd2 | cmd3 | ... | cmdn &

where the cmd{1..n} may not be distinct, how do I get the PID of cmdn? Alternatively, how can I signal the cmdn process? (For example, send it SIGUSR1?) pkill/pgrep, pidof etc. don't look like good answers, since other instances of cmdn maybe running, including as part of the same pipeline. jobs -p gives the PID of cmd1, for me.

Get PID of any command in backgrounded piped sequence of commands

If, in bash, I execute:

cmd1 | cmd2 | ... | cmdi | ... | cmdn &

where the cmd{1..n} may not be distinct, how do I get the PID of cmdi? Alternatively, how can I signal the cmdi process? (For example, send it SIGUSR1?) pkill/pgrep, pidof etc. don't look like good answers, since other instances of cmdi maybe running, including as part of the same pipeline. jobs -p gives the PID of cmd1, for me.

i can be anything in {1..n}.

Source Link
muru
  • 78.1k
  • 16
  • 213
  • 319

Get PID of last command in backgrounded piped sequence of commands

If, in bash, I execute:

cmd1 | cmd2 | cmd3 | ... | cmdn &

where the cmd{1..n} may not be distinct, how do I get the PID of cmdn? Alternatively, how can I signal the cmdn process? (For example, send it SIGUSR1?) pkill/pgrep, pidof etc. don't look like good answers, since other instances of cmdn maybe running, including as part of the same pipeline. jobs -p gives the PID of cmd1, for me.