我正在尝试shell_exec()
作为 WordPress 插件的一部分运行后台 PHP 进程。
这是样本。
/* 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 &');
}
我究竟做错了什么?
由于我在 Windows 服务器上进行测试,因此路径php
in被我的实际脚本中的 php.exe 路径更改。shell_exec()
它在其他 PHP 脚本中运行良好。
谢谢。