4

My question may be a bit complicated.

This is my scenario:

I want to start the mjpg streamer using php. My php script is:

<?php 
$output = shell_exec("sh /var/www/html/shellstart.sh");
echo "<pre>$output</pre>";
header("Location: ../index.php");
?>

It works fine in starting the script. The problem is that the script

shellstart.sh:

LD_LIBRARY_PATH=/opt/mjpg-streamer/mjpg-streamer-experimental/ /opt/mjpg-streamer/mjpg-streamer-experime $

starts a process and the header() function is never called, because it never leaves the script. So I am looking for a method to start the script, keep it running but return to the php script and call the header() function.

I already tried

trap "exit" SIGINT

and

nohup LD_LIBRARY_PATH=/opt/mjpg-streamer/mjpg-streamer-experimental/ /opt/mjpg-streamer/mjpg-streamer-experime$

also I tried $ disown. But when using nohup or $ disown the script won't even start the process.

I appreciate any help! Thanks in advance.

Steven Carlson
  • 925
  • 1
  • 10
  • 25
kingardox
  • 127
  • 11
  • Possible duplicate with http://stackoverflow.com/questions/222414/asynchronous-shell-exec-in-php – IeuanG Feb 19 '16 at 23:23

1 Answers1

5

You want to try something like:

$pid = exec("sh /var/www/html/shellstart.sh> /dev/null 2>&1 & echo $!; ", $output); 

This will make the script start in the background and return you the PID of the script.

Emil Borconi
  • 3,326
  • 2
  • 24
  • 40