I'm trying to use shell_exec()
and having hard time understanding how to use it. I referred to this page: Is there a way to use shell_exec without waiting for the command to complete?.
I created two PHP files:
shell_exec.php
<?php
shell_exec('php ' . __DIR__ . '/log.php > /dev/null 2>/dev/null &');
?>
log.php
<?php
$file = __DIR__ . '/log.txt';
$current = time() . ": sample text\r\n";
file_put_contents($file, $current,FILE_APPEND);
?>
Then I accessed the page of shell_exec.php from my browser. I expected that it runs log.php and creates a file. However, nothing happens.
I suspect that the php paths specified in the function, which is php
in the above code, is different in different operating system. I'm currently testing in Windows, so that may be the reason that it doesn't work. But I'm not 100% sure.
If so, how can I find the php path and specify it in my environment? Also does that mean that if I distribute a script which uses shell_exec()
and call a php script, do I have to write some code to find out the php path? In that case, is there a good way of doing it?