I have a script which registers a SIGINT trap and starts a ffmpeg background process that records part of the screen. The SIGINT trap sends a SIGINT signal to the background ffmpeg process to get it to gracefully stop and finish the recording.
When this script is run in a terminal, and terminated from a separate terminal with kill -INT [SCRIPT_PID]
, the ffmpeg background process terminates gracefully and outputs confirmation in terminal 1.
When the script is run in a terminal and stopped with ctrl+c
the background process just dies instantly. (even if ctrl+c should just send a SIGINT signal)
Why does ctrl+c behave differently than killing the script with kill -INT
in this case?
How can i make sure the ffmpeg background process ends gracefully when ending the script with ctrl+c?
#!/bin/bash
exit_script() {
kill -INT $ffmpeg_pid
wait $ffmpeg_pid
printf "\n\nffmpeg should say 'exiting normally, received signal 2' before this message is printed!\n\n"
}
trap exit_script SIGINT
ffmpeg -f x11grab -s 500x500 -i :0.0+0,0 ~/video_`date +%s`.webm &
ffmpeg_pid=$!
wait
edit: it seems like ffmpeg receives 2 int signals in the case of ctrl+c
, but i don't know why