Dikens
Dikens

Reputation: 1

Bash output tracking

I made an infinite loop in the Bash script to restart streaming.

#!/bin/bash
while true
  do
    ffmpeg -re -rtsp_transport tcp -i "rtsp://{address:port}/{key}" -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=44100 -c:a libmp3lame -ab 128k -ar 44100 -c:v copy -threads 2 -bufsize 512k -f flv "rtmps://{address:port}/live/{key}" &
    PID=$!
    sleep 12h
    kill $PID
    sleep 2
 done

But sometimes an error occurs... which is displayed in the console in the following form:

rtsp://{address:port}/{key}: Invalid date found when processing input

And this time the stream does not work. How to supplement my script, which would track after running the command output in the console and when the error Invalid date found when processing input will end the process and immediately restart the stream?

Upvotes: 0

Views: 144

Answers (1)

glenn jackman
glenn jackman

Reputation: 247062

If ffmpeg wasn't launched in the background this would be a lot easier: you'd just have to check the exit status.

But because we're dealing with multiple processes, we'll have to incorporate some inter-process communication.

Using signals: this is untested:

#!/bin/bash

# set up a signal handler for SIGUSR1
trap 'echo "ffmpeg died!"; exit 1' USR1

while true; do
    (   # start a subshell
        # if ffmpeg exits non-zero, signal the parent shell
        ffmpeg -options... || kill -s USR1 $$
    ) &
    # rest of loop is the same
done

Upvotes: 1

Related Questions