1

Okay, I've got a program I'm trying to fix, for lengthy reasons a rewrite isn't the greatest option... So, I've been up for 8 hours trying to solve this and I'm completely stumped.

I'm trying to get the PID of the ffmpeg in this nested subshell command:

(touch ~/addThumSlate.1BUSY.FILE; echo "" | ffmpeg -I "~/thumb_0011374_402.mov" "~/0011374/402_slated.mxv" > "~/0011374_402-addThumSlate1.txt" 2>&1; rm -f ~/addThumSlate1.BUSY.FILE) >/dev/null 2>&1 & echo $!

Obviously the final " & echo $!" does not return the pid of the ffmpeg command, and "echo $!" doesn't work within a subshell, and I tried messing around with echo $$; ( : ; bash -c 'echo $PPID' ) but that was only retuning me the parent shell for some reason?

Caveat, this is bash 3.2 running on a Mac, so $BASHPID is out. Also, I can't ps | grep 'ffmpeg', as there are multiple ffmpegs running. I've trolled the interwebs trying to find a solution.

Hoping you can help,

 -Sleepless in a subshell
1
  • 3
    foo could be replaced with sh -c 'echo $$ > /some/pidfile; exec foo' – A.B Aug 8 '20 at 13:11
0

$! should be the pid of the subshell. Try pstree -p $! to see if the information you need is in the list.

You may need to parse it (maybe in awk), and the output to a pipe is different to what you see on screen.

Example from my login shell. You should see one ffmpeg in your list.

paul $ pstree -p $PPID | cat -vet
mate-terminal(6849)-+-bash(6856)---vi(9107)$
                    |-bash(7338)$
                    |-bash(9456)-+-cat(10573)$
                    |            `-pstree(10572)$
                    |-{dconf worker}(6855)$
                    |-{gdbus}(6854)$
                    `-{gmain}(6852)$
paul $

You may need to download pstree from the Mac Store: http://macappstore.org/pstree/

Typical awk ending to store the Pid in a variable would be:

Pid=$( pstree -p $PPID | awk -v 'RS=)' -v 'FS=(' '/ffmpeg/ { print $NF }' )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.