Rerun command if it die in elixir

Hi, I use m3u8 to live stream audio, I use ffmpeg to convert from webrtc to m3u8. but some reason ffm (port not avaiable) can not start or it die when running. I want to Rerun it again with other port.
I use Supervisor and GenServer to do it. here my code:

defmodule FFmpeg.Supervisor do
  use Supervisor

  def start_link do
    Supervisor.start_link(__MODULE__, [])
  end

  def init(_) do
    children = [
      worker(FFmpeg.Service, [])
    ]

    supervise(children, [strategy: :one_for_one])
  end
end
defmodule FFmpeg.Service do
  use GenServer

  def init(:ok) do
    {:ok, :ok}
  end

  def start_link do
    GenServer.start_link(__MODULE__, :ok, name: __MODULE__)
  end

  def start_ffmpeg(room) do
    GenServer.call(__MODULE__, {:check, room})
  end

  def handle_call({:check, room}, _from, state) do
    exec = "ffmpeg -analyzeduration 300M -probesize 300M -i #{sdp_file} -strict -2 -c:a aac -ar 16k -ac 1 -preset ultrafast -tune zerolatency  -f flv #{path_rtmp_janus}/stream_#{room.id} -nostdin -nostats >/dev/null 2>&1 &"
    Port.open({:spawn, exec}, [:binary])  # how to RERUN if it die
    IO.inspect "========= END run ffmpeg =============="

    {:reply, :ok}
  end
end

and run by:

if !Process.whereis(FFmpeg.Service) do
        FFmpeg.Supervisor.start_link()
end

 FFmpeg.Service.start_ffmpeg(room)

Typically we would do the following: Port.open returns a port(). You can monitor this port() using Port.monitor/1
If ffmpeg dies it will send a {:DOWN, ref, :port, object, reason} to GenServer.handle_info allowing you to act on it.

I added this code:

port = Port.open({:spawn, exec}, [:binary])
Port.monitor(port)

....
def handle_info({:DOWN, ref, :port, object, reason}, state) do
    IO.inspect "========= DOWN =============="
    IO.inspect reason
    {:noreply, state}
end

it always run to :DOWN with reason :normal, I change exec query to error it continue run the same

Can you force ffmpeg to terminate unexpectedly to see if the “reason” changes ?

I try it, but it same, I change wrong command to exec, but it not change reason

Given this you might have to call a shell script which exec ffmpeg. The shell script can traps it dying unexpectedly and returning an error code if that occurs.

erlexec or porcelain can do that for you as well with their helper addons.

I am wrong with command, I remove >/dev/null 2>&1 & and it work