Unix/Windows,设置后台进程?从PHP代码 [英] Unix/Windows, Setup background process? from php code

查看:93
本文介绍了Unix/Windows,设置后台进程?从PHP代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我从 http://php.net/manual/zh/找到了一个函数function.exec.php

function execInBackground($cmd) {
if (substr(php_uname(), 0, 7) == "Windows"){
    pclose(popen("start /B ". $cmd, "r")); 
}
else {
    exec($cmd . " > /dev/null &");  
}
}

Windows使用情况:

pclose(popen('start /B php path/to/background_cron.php', 'r'));

Unix使用情况:

exec('php path/to/background_cron.php >> path/to/background_error.log &')

但是当调用这些行时,实际上没有任何反应(等待5分钟左右).难道我做错了什么?在激活这些行之前,我有一个file_exists检查我的path/to/background_cron.php是否存在,如果不存在,则会返回错误.

在实时UNIX服务器上,将所有文件夹权限设置为对所有者,组,公用(0755)进行写入,读取,执行,并且将文件许可权设置为对所有者,组,公用(644)进行读取.在unix命令运行后,background_log变为空,它将创建4个新的空行(\ n),但不返回任何期望值

解决方案

在Windows中有点奇怪的是,由于Web服务器以不同的用户身份运行,因此它将在自己的用户空间(而不是桌面环境)中启动应用. /p>

该进程可能正在启动并且不在您的进程列表中-请为所有用户检查该进程.另外,请检查事件日志,以防它试图运行但抛出异常

另一种可能性是Web用户没有执行该命令的权限-查看谁正在运行应用程序池并检查其对那些文件夹的权限.

请注意,不要通过允许Web服务器运行任意代码来创建安全漏洞.

So I found a function from http://php.net/manual/en/function.exec.php

function execInBackground($cmd) {
if (substr(php_uname(), 0, 7) == "Windows"){
    pclose(popen("start /B ". $cmd, "r")); 
}
else {
    exec($cmd . " > /dev/null &");  
}
}

Windows Usage:

pclose(popen('start /B php path/to/background_cron.php', 'r'));

Unix Usage:

exec('php path/to/background_cron.php >> path/to/background_error.log &')

But when these lines are called nothing actually happens (after waiting 5 or so minutes). Am I doing something wrong? prior to these lines being activated I have a file_exists checking if my path/to/background_cron.php exists and it if it doesn't it will return a error.

On the live unix server all the folder permissions are set to write,read,execute for owner,group,public (0755) and file permissions are set to read for owner,group,public (644). The background_log comes up empty after the unix command runs, it creates 4 new blank lines (\n) but doesn't return any expections

解决方案

One thing that's a little odd in windows is that since the webserver is running as a different user, it launches apps in its own userspace - not your desktop.

It's possible the process is launching and isn't in your process list - check it for all users. Also, check the event log in case it's trying to run but throwing an exception

Another possibility is that the web user doesn't have permissions to execute the command- Have a look at who the app pool is running as and check their permissions to those folders.

Be careful you don't create security holes by allowing the webserver to run arbitrary code.

这篇关于Unix/Windows,设置后台进程?从PHP代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆