使用shell_exec()的后台PHP进程作为WordPress插件的一部分

时间:2012-09-15 10:40:36

标签: php wordpress-plugin background-process wordpress

我正在尝试使用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中的路径shell_exec()被我实际脚本中的php.exe路径所改变。它在其他PHP脚本中运行良好。

感谢。

1 个答案:

答案 0 :(得分:2)

AFAIK(据我所知)/ dev / null在Windows中不存在 只需使用NUL ......例如

shell_exec('php "' . ABSPATH . '/index.php" > NUL &');