I'm trying to run a background PHP process with shell_exec() as a part of WordPress plugin.
This is the sample.
/* Plugin Name: Sample Background Process */
add_action('init', 'Sample_Background_Process');
function Sample_Background_Process() {
$file = __DIR__ . '/log.html';
$current = date('l jS \of F Y h:i:s A') . ': accessed<br />';
file_put_contents($file, $current, FILE_APPEND);
}
It creates a log in the plugin folder and whenever a page gets access it appends some text to it.
Then, I added this line, shell_exec('php "' . ABSPATH . '/index.php" > /dev/null 2>/dev/null &');. So a background process should access the page and run the function which appends text to the log file. But it doesn't seem to be doing it. I expect one page load produces two lines in the log file. But it only adds one line, which means a background process is not run or WordPress does not do anything if it is called like this.
add_action('init', 'Sample_Background_Process');
function Sample_Background_Process() {
$file = __DIR__ . '/log.html';
$current = date('l jS \of F Y h:i:s A') . ': accessed<br />';
file_put_contents($file, $current, FILE_APPEND);
shell_exec('php "' . ABSPATH . '/index.php" > /dev/null 2>/dev/null &');
}
What am I doing wrong?
The path, php in shell_exec() is altered by the path to php.exe in my actual script since I'm testing on Windows server. It runs fine in other PHP scripts.
Thanks.
if( ini_get('safe_mode') )returns false.shell_execand executing it manually./dev/null 2>/dev/nulldoesn't work at all in Windows. I thought it would have worked but as I view the other test scripts that I have created before, they were using2>&1. So, thanks, all became clear now.