我试图运行一个背景PHP过程与shell_exec()
作为WordPress插件的一部分。背景PHP过程与shell_exec()作为WordPress插件的一部分
这是样本。
/* 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);
}
它创建一个日志中的插件文件夹,每当一个页面获取访问其追加一些文本。
然后,我添加了这一行,shell_exec('php "' . ABSPATH . '/index.php" > /dev/null 2>/dev/null &');
。所以后台进程应该访问该页面并运行将文本附加到日志文件的功能。但它似乎并没有这样做。我期望一个页面加载在日志文件中产生两行。但它只会添加一行,这意味着后台进程不会运行,或者WordPress不会执行任何操作,如果这样调用。
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 &');
}
我在做什么错?
路径php
在shell_exec()
由于我在Windows服务器上测试而在我的实际脚本中被路径改为php.exe。它在其他PHP脚本中运行良好。
谢谢。
你肯定安全模式是Windows服务器上禁用? –
'if(ini_get('safe_mode'))'返回false。 – Teno
尝试打开错误报告并自行运行该单一行。这应该让你知道发生了什么。您可能还想打印出您传递给'shell_exec'的字符串并手动执行它。 –