Unix/Windows, Setup background process? from php code

百般思念 提交于 2019-12-23 03:58:10

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/9252410/unix-windows-setup-background-process-from-php-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!