2012-09-15 18 views
0

我试图运行一个背景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 &'); 
} 

我在做什么错?

路径phpshell_exec()由于我在Windows服务器上测试而在我的实际脚本中被路径改为php.exe。它在其他PHP脚本中运行良好。

谢谢。

+0

你肯定安全模式是Windows服务器上禁用? –

+0

'if(ini_get('safe_mode'))'返回false。 – Teno

+0

尝试打开错误报告并自行运行该单一行。这应该让你知道发生了什么。您可能还想打印出您传递给'shell_exec'的字符串并手动执行它。 –

回答

2

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

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

它似乎工作。谢谢。我意识到它创造了一个无限的过程循环。 – Teno

+0

以及...无限循环不是我所例外:) – user1494162

+0

在Windows中使用shell_exec是一个坏主意 –