There is such a script:

#! /bin/bash while true; do camscr=( 'ffmpeg -i rtmp://127.0.0.1/live/cam1 /web/www/preview/cam1.jpeg -y' 'ffmpeg -i rtmp://127.0.0.1/live/cam2 /web/www/preview/cam2.jpeg -y' 'ffmpeg -i rtmp://127.0.0.1/live/cam3 /web/www/preview/cam3.jpeg -y' ) camcnt=0 until [ $camcnt = ${#camscr[@]} ] do ${camscr[$camcnt]} (( camcnt++ )) sleep 15 done done 

For ${camscr[$camcnt]} you need to make a time limit in case ffmpeg hangs. In addition to these ffmpeg , several dozens of ffmpeg are spinning, so you cannot kill them all at once.

I want to start these processes in turn either before successful completion, or with forced termination in 10-15 seconds with an alternate interval of 15 seconds.

  • You can run ffmpeg in the background and then watchdog for it, which will send this process a kill by timeout or will end itself when this ffmpeg ends - avp
  • and the error message is better to send to ffmpeg developers. state what you found: that the script construction timeout ΠΈΠ½Ρ‚Π΅Ρ€Π²Π°Π» ffmpeg ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹ does not work. - aleksandr barakin

2 answers 2

  1. you can do without the counter, going through the elements of the array directly:

     for cam in "${camscr[@]}"; do $cam done 
  2. You can start all processes at once in a nested loop (using the & operator), and after the loop, after waiting the required number of seconds, β€œkill” all child processes:

     for cam in "${camscr[@]}"; do $cam & done sleep ΠΈΠ½Ρ‚Π΅Ρ€Π²Π°Π» pkill -P $$ 

explanation:

  • The pkill program is usually installed with the base system (in debian gnu / linux and legacy distributions, this program is included in the procps package).
  • The -P ΠΈΠ΄Π΅Π½Ρ‚ΠΈΡ„ΠΈΠΊΠ°Ρ‚ΠΎΡ€ option indicates "send a signal to all child processes of the process with the given identifier." the default signal is SIGTERM , another signal can be specified using the option -имя_ΠΈΠ»ΠΈ_Π½ΠΎΠΌΠ΅Ρ€ (or --signal имя_ΠΈΠ»ΠΈ_Π½ΠΎΠΌΠ΅Ρ€ ), for example -9 or -KILL or --signal 9 .
  • $$ - in posix-compatible shells, this variable contains the identifier of the current process (that is, the process that executes the script).
     $ timeout --help Usage: timeout [OPTION] DURATION COMMAND [ARG]... or: timeout [OPTION] Start COMMAND, and kill it if still running after DURATION. Mandatory arguments to long options are mandatory for short options too. --preserve-status exit with the same status as COMMAND, even when the command times out --foreground when not running timeout directly from a shell prompt, allow COMMAND to read from the TTY and get TTY signals; in this mode, children of COMMAND will not be timed out -k, --kill-after=DURATION also send a KILL signal if COMMAND is still running this long after the initial signal was sent -s, --signal=SIGNAL specify the signal to be sent on timeout; SIGNAL may be a name like 'HUP' or a number; see 'kill -l' for a list of signals --help display this help and exit --version output version information and exit DURATION is a floating point number with an optional suffix: 's' for seconds (the default), 'm' for minutes, 'h' for hours or 'd' for days. If the command times out, and --preserve-status is not set, then exit with status 124. Otherwise, exit with the status of COMMAND. If no signal is specified, send the TERM signal upon timeout. The TERM signal kills any process that does not block or catch that signal. It may be necessary to use the KILL (9) signal, since this signal cannot be caught, in which case the exit status is 128+9 rather than 124. GNU coreutils online help: <http://www.gnu.org/software/coreutils/> Full documentation at: <http://www.gnu.org/software/coreutils/timeout> or available locally via: info '(coreutils) timeout invocation' 
    • please give an example of use - mymedia
    • By the way, there was a long discussion about the launch of the timeout ΠΈΠ½Ρ‚Π΅Ρ€Π²Π°Π» ffmpeg ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹ . so the author of the question inside the script did not work. from the discussion was only my final comment . - aleksandr barakin