0

To run one command in the background, it works well.

$cmd = 'ffmpeg -re -i ./97.mp4 -vcodec copy -acodec copy -f flv -y rtmp://example.com/c/190843?auth_key=7e2682b5 > output 2>&1 </dev/null &';
exec($cmd, $output, $return_var);

Then I need to sleep for some time before the ffmpeg command. I refer to How do I run multiple background commands in bash in a single line? which works well directly in the bash console.

While not works in the below PHP script, which will return when the bash command finishes running.

$cmd = '(sleep 5; ffmpeg -re -i ./97.mp4 -vcodec copy -acodec copy -f flv -y rtmp://example.com/c/190843?auth_key=7e2682b5 > output 2>&1 </dev/null) &';
exec($cmd, $output, $return_var);
LF00
  • 27,015
  • 29
  • 156
  • 295

2 Answers2

1

I think you also need to handle sleep's stdout/stderr.

( sleep 5 > /dev/null 2>&1; ...; ) &

Or you can put the redirection after ( ... ):

( sleep 5; ffmpeg ...no redir here...; ) < /dev/null > /dev/null 2>&1 &
pynexj
  • 19,215
  • 5
  • 38
  • 56
0

To run multiple commands from on bash command; concatenate using &&.

For instance:

sleep 5 && echo hello && sleep 2 && echo world

This trick can be particularly useful in cron tasks to be able to sequence multiple items within the same minute, as a different sleep value can be used as an offset before starting the command.

Howard Tomlinson
  • 71
  • 1
  • 2
  • 5