0

Good day to all, thanks to all supporting stackoverflow, learned a lot here. Upfront, I am a hobbist learning day by day.

I have bash script which works so far one time. Let me explain in short what it does.

I am logging my "application" into tmp file jctl. This application logs changes within a system if a URL request is changing by user action and adding it into jctl as long log entry. Then with a combination of grep/tail I finally filter the usable URL which is my variable "var0". Means "var0" is changing from time to time. Three conditions are possible

  • var0 URL is equal - do nothing
  • var0 URL has changed - kill running ffmpeg and restart ffmpeg with new URL from var0
  • var0 is empty - kill running ffmpeg and wait for new URL from var0

The while true loop generating var0 is working, the part of handover to ffmpeg , start/stop ffmpeg does work one time, when I start the script. When var0 changes or is empty the URL started with script first time is executed.

Expected behavior is: The while loop runs inifinte, updates variable var0 based on the logs in jctl file, and start/stops ffmpeg according the var0 criteria described above.

I hope anyone can help or give the right hint. Thanks in advance.

What it does: It fetches on demand a HLSv5 manifest with separate Video and Audio m3u8 and ffmpeg muxes them to one mpgets stream.

My script so far:

#!/bin/sh

journalctl -fu application -o cat > /tmp/jctl &

while true
do
  cat /tmp/jctl | grep "playing" | tail -n1 | tee /tmp/all

  grep -Eo "(prox/http|prox/https)%3a//\[a-zA-Z0-9./?=\_%:-\]\*(:)" /tmp/all |
    tail -n1 | sed 's,:,,g' | sed 's,prox/https%3a,https:,g' | tee /tmp/var0

  var0=$(sed -n "1p" /tmp/var0)
  if [ ! -z "$var0" ] # URL not empty
  then
    curl -L $var0 | tee /tmp/tmp
    var1=$(sed -n '/RESOLUTION=1/{n;p}' /tmp/tmp | sed -n "1p")
    var2=$(sed '/DEFAULT=YES[^[]*URI="/!d;s//&\n/;s/.*\n//;:a;/"/bb;$!{n;ba};:b;s//\n&/;P;D' /tmp/tmp | sed -n "1p")
    `ffmpeg -re -rtbufsize 8M -i $var1 -i $var2 -c copy -f mpegts udp://227.0.0.1:1234?pkt_size=1316 &`
  fi
done  
4
  • great ! now you can clean up the unnecessary `\` you had added to let your characters show up correctly in "normal" mode ... :) Commented Dec 7, 2022 at 8:03
  • ups, corrected too, hopefully. :) Commented Dec 7, 2022 at 8:08
  • @NewbiDam I formatted your script, so that we can read it better. Of course I kept the syntax exactly like it was before. But I'm unsure if the backticks (`) in the line with ffmpeg -re ... & are actually part of your script (which would be a bug) or if they were only used for formatting code in this question. Can you please check the script in your question again and either correct it or confirm that this is the actual script you are executing? In the latter case, please try to execute your script without the backticks too. Commented Dec 7, 2022 at 8:29
  • 1
    Also try shellcheck.net to fix other potential sources of errors, e.g. the unquoted variables. Commented Dec 7, 2022 at 8:37

1 Answer 1

1

To be able to kill ffmpeg command you have to get it pid, grep it or better store it in special var at the moment of running ffmpeg in bg, like this:

ffmpeg -re ... 1316 & ffmpeg_pid=$!

$ffmpeg_pid var will store your ffmpeg command's pid.

kill $ffmpeg_pid

And to check that url changed you have to store previous value somewhere, so when you get $var0 store it in $old_url var for example and than check:

[[ $var0 != $old_url ]] && echo 'url changed'
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Ivan! The hint with ffmpeg was very good! The check var0 vs oldvar0 I already had in this manner. But I failed stupidly, how to store var0 as old_var0 to compare with new var0. I hope know what I mean.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.