-3

I have a bash script that is called by a phone system that gets some audio from a URL and (using ffmpeg, mplayer etc.) then pipes it back to the application. The file can have several URL's that are called so if the first one say goes off line or gives a 404 it will go to the next line.

I have an issue where some times the server will produce content however there is no audio. In such a case I want to kill the current PID of ffmpeg, mplayer etc. so that the script should move on.

I can't foreground it and get the last PID since once it's ran in the foreground the media is no longer being piped to the application calling it. I can't use exec in the beginning since if I then issue a kill to the PID the script dies which I don't want.

The script looks something like this:

#!/bin/bash

/usr/bin/ffmpeg  -i 'http://1.1.1.1/soft_music' -vn -ar 8000 -ac 1 -f s16le -
/usr/bin/ffmpeg  -i 'http://2.2.2.2/soft_music' -vn -ar 8000 -ac 1 -f s16le -

I assume I need to add something that will allow me to log the pid of the current ffmpeg command running so my external script can get it and kill it. Once that's done it will go to the next line and try the next stream from 2.2.2.2

shellter
  • 36,525
  • 7
  • 83
  • 90
Dovid Bender
  • 139
  • 9
  • This might help https://serverfault.com/questions/205498/how-to-get-pid-of-just-started-process – mrflash818 Dec 17 '17 at 02:31
  • @mrflash818 I can't do that. As I mentioned as soon as I have & it puts the current process into the background so the app no longer gets the output from ffmpeg – Dovid Bender Dec 17 '17 at 02:38
  • 2
    Voting to close as **Too broad** : "Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the [How to Ask page](https://stackoverflow.com/help/how-to-ask) for help clarifying this question." Please read https://stackoverflow.com/help/on-topic , https://stackoverflow.com/help/dont-ask , https://stackoverflow.com/help/mcve and take the [tour](https://stackoverflow.com/tour) before posting more Qs here. Good luck. – shellter Dec 17 '17 at 02:52

1 Answers1

0

It seems the script below would do what I am looking for:

#!/bin/bash

/usr/bin/ffmpeg  -i 'http://1.1.1.1/soft_music' -vn -ar 8000 -ac 1 -f s16le - & echo $! > /tmp/my_pid
%1
/usr/bin/ffmpeg  -i 'http://2.2.2.2/soft_music' -vn -ar 8000 -ac 1 -f s16le - & echo $! > /tmp/my_pid
%1

What this does is put the ffmpeg process into the background just for one moment to store the PID in a file. As soon as it goes to the next line we recover it with the job ID which will be 1.

Dovid Bender
  • 139
  • 9
  • 2
    job control in a script is **not** necessary **nor** is it appropriate here: https://stackoverflow.com/questions/690266/why-cant-i-use-job-control-in-a-bash-script. Simply save the pid to a variable, or better yet, run commands synchronously and validate the command's stdout and exit if valid. (question is unclear) – Travis Clarke Dec 17 '17 at 03:33