Knowledge Base
PHP
- Details
- Parent Category: Knowledge base
- Category: PHP
Trying to run a Background PHP process on a web page that will return its output in realtime?
Try something like this example of a basic shell execution from PHP using proc_open():
$cmd = "ping 127.0.0.1"; $descriptorspec = array( 0 => array("pipe", "r"), // stdin is a pipe that the child will read from 1 => array("pipe", "w"), // stdout is a pipe that the child will write to 2 => array("pipe", "w") // stderr is a pipe that the child will write to ); flush(); $process = proc_open($cmd, $descriptorspec, $pipes, realpath('./'), array()); echo "<pre>"; if (is_resource($process)) { while ($s = fgets($pipes[1])) { print $s; flush(); } } echo "</pre>";
if you want to use the PHP exec(), you can send output to a file and read the last line of output file using ajax calls at regular intervals or using SSE:
$result = exec('$cmd > /path/to/file &');
The ampersand at the end will make it run in the background and you can read the output in /path/to/file