I am trying to get the pid of the execution command, instead it is returning pid of the tee command.
For example:
echo "$$"; exec some_script.sh | tee output.txt
It returns pid of the tee command instead of returning exec some_script.sh pid. Is there a way to return the pid of the exec some_script.sh.
I believe the posted seqence will show the PID of the pipeline parent - usually the bash interpreter used to launch the command (and not the PID of the tee).
Two options to address this:
1. consider modifying the command to:
(echo "$BASHPID" ; exec some_script.sh) | tee output.txt
The '$$' tracks the PID of the parent bash. BASHPID tracks the PID of the subshell used for the first part of the pipe, which will be used to run the script, because of the `exec.
2. Using command substitution
some_script.sh > >(tee output.txt) &
SCRIPT_PID=$!
echo "SCRIPT_PID=$SCRIPT_PID"
This option eliminate the explicit pipe. The major advantage is let the calling bash know what is the script PID, making it possible to use it for additional commands (wait, kill, ...). No need to parse the PID from the output.txt.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With