Reputation: 1840
I have a shell script that runs a Java process p1 and also there's a non-Java process p2 that takes input from p1.
How can I get process id of p1?
I have a shell script that works unpredictably (sometimes it works, sometimes it doesn't). I have browsed the net but none of the answers seem perfect.
My script:
nohup sh -c "exec java p1 | p2 2>&1" &
$pid=`echo $!`
my_pid=exec ps -eo "%p %c %P" | awk -v p=$pid 'p==$3{print $1 $2}' | grep java | sed -e 's/java//'
echo "my_pid $my_pid"
Upvotes: 0
Views: 644
Reputation: 47233
This is a simplified version of mmd's answer from the question i linked to:
{ java p1 & echo $! >&2; } | p2 2>&1 &
This prints the PID of p1 on standard error. You also get a message from the shell telling you that the echo command has finished, but you can ignore that.
Upvotes: 2