0

currently, I am working on a project where I need to run rtsp to rtmp conversion using ffmpeg. but some how I am getting error after several minutes running the process and the process getting exited. I want to run the process even if it gets error. I am using this command ffmpeg -re -rtsp_transport tcp -i rtsp://my-link -c:v copy -c:a aac -ar 44100 -ac 1 -f flv -flvflags no_duration_filesize rtmp://my-url

I tried this bash script.

#!/bin/bash
cmd="ffmpeg -re -rtsp_transport tcp -i $1 -c:v copy -c:a aac -ar 44100 -ac 1 -f flv -flvflags no_duration_filesize $2"
while true; do $cmd && break; done

using this script I am able to run the process again if I get any error. but the problem is it doesn't stop even if I run kill -9 my-pid. it just re-runs the process.

shellter
  • 36,525
  • 7
  • 83
  • 90
  • try killing the PID for that wrapper script. `ps -ef | grep myWrapperScript` . Be sure to skip the line returned were `grep` is the process. Good luck. – shellter Jan 25 '23 at 23:35
  • I can have running multiple processes at the same time. If I run `ps -ef | grep myWrapperScript` then won't it kill all the process? I don't want to kill all the process. – Samsul Islam Jan 26 '23 at 07:52
  • you can run the code I have provided and nothing bad will happen, it only displays what jobs are running. If you ave more than one, then you have to look at more details about the job to know which one to kill. This is not defined as part of your problem in your question above. Take the time to learn about what `ps` and all it's options can help you. You will need this if you're going to be running multiple "copies" of the same script. Sorry, can't spend more time on this. Good luck. – shellter Jan 26 '23 at 14:15
  • I ran this (`ps -ef | grep myWrapperScript`) command. it only showing me the processes that I am running. but I don't need to show that. I am already storing process pid to stop the process later. I am using this (`exec("/var/www/html/stream.sh $VIDSOURCE $DESURL" . ' > ' . $logPath . ' 2>&1 & echo $!; ', $output, $return);`) php code to run the script. but the problem is when I try to stop the process it runs again with another pid. I want to prevent this. and stop the process at the first attempt of killing the process – Samsul Islam Jan 27 '23 at 09:36
  • Arg, this is running from inside `php`? This is a key bit ino that was needed. But anyway, `man ps` and search for `tree`. You need to see how this process tree is formed. You should see the `php` process, and the bash process AND the `ffmpeg` process. You will likely see 2 bash processes (for each $cmd) below the php. You need to kill the topmostl AND because you have a `while true` loop, this will always restart the `$cmd` string. To test if that is the culprit, remove the `while true` loop wrapper and just let the "$cmd" run one time. Check it with your newly discovered `ps` tree. .... – shellter Jan 27 '23 at 15:49
  • I found 1 bash process for one `$cmd`. and I also find that if I kill it first time then it runs again and if I kill that re-run process then it does not starts again. So, If I just kill that second time process then my problem solved. thanks for your effort. – Samsul Islam Jan 29 '23 at 12:16

0 Answers0