0

I'm using livestreamer and ffmpeg to grab images from a live stream at 0.1 fps:

livestreamer --player "ffmpeg -i" --player-args "{filename} -vf fps=1/10 out%04d.png" https://www.ustream.tv/channel/number worst 

I couldn't get ffmpeg to overwrite its output, so it keeps creating new files with the out%04d.png pattern. This is fine, I can keep track of the newest out file and delete old ones. However, the livestreamer process sometimes exits for various reasons and I'd like to restart it automatically as long as I want it to run.

An attempt using a subshell didn't react to CTRL-C as I expected because the signal is not passed on the subshell:

#!/bin/bash ( while true; do livestreamer --player "ffmpeg -i" --player-args "{filename} -vf fps=0.1 out%04d.png" https://www.ustream.tv/channel/number worst done ) & while true; do newestImage=${code to find newest image} postProcess $newestImage deleteOldImages sleep 5 done 

When I send SIGINT to above script, the subshell stays alive even when I close the shell in which I started the parent script. The output directory is flooded with new files while the code that is supposed to delete them is not running any more. A trap didn't help either:

trap "kill $SUBSHELL_PID" SIGINT ( ... ) & # subshell as above SUBSHELL_PID=&! 

With that I wasn't even able to interrupt the parent script. Obviously, I'm not experienced with signal handling in bash at all, so I'm happy about any advice that gets me forward. I need to restart the frame grabber as long as the parent script is running, and keep track of old files in order to delete them.

Edit: Might the subshell be a bad idea in the first place? Can I somehow launch livestreamer in the background and either kill it when I want, or restart it when it exited by itself?

1
  • 1
    Once you know the PID of the subshell, with a single grep or awk command you can get which livestreamer processes were started by that and kill only those livestreamer(s) in the trap. Commented Sep 13, 2016 at 13:00

1 Answer 1

1

Kill livestreamer itself.

trap "killall livestreamer" SIGINT 
3
  • Does that not kill any instances of livestreamer outside of my parent script? I'm not having any yet, but I might in the future Commented Sep 13, 2016 at 8:15
  • Your whole solution is a quick dirty hack. In the future write a correct error handling instead of pressing ctrl-c yourself a dozen times a day. Commented Sep 13, 2016 at 8:18
  • I'm well aware of that, and I'm willing to improve it. Your comment, however, is not helpful in that respect, and does not answer the question I have regarding your answer. Commented Sep 13, 2016 at 8:28

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.