Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start shell script using php and exit it without stopping the process

Tags:

shell

php

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.

like image 234
kingardox Avatar asked Oct 21 '25 09:10

kingardox


1 Answers

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.

like image 102
Emil Borconi Avatar answered Oct 22 '25 23:10

Emil Borconi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!