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?