使用dev> null&运行exec()命令不起作用,但是dev> null起作用 [英] Running exec() command with dev>null & doesn't work, but dev>null does
问题描述
我有这个exec命令,它运行得很好,但是不会释放浏览器(即,存在php超时问题,但实际命令仍在运行):
I have this exec command which runs just fine, but doesn't free up the browser (ie. there is a php timeout issue, but the actual command continues to run):
exec("/usr/local/php53/bin/php csv.php $file $user > /dev/null");
当我运行ps auxw
时,我看到了两个正在运行的进程:
When I run ps auxw
I see these two running processes:
sh -c /usr/local/php53/bin/php csv.php /tmp/php9Pwu9e 294 >
/usr/local/php53/bin/php csv.php /tmp/php9Pwu9e 294
但是,当我运行下面的代码时,浏览器立即是免费的,但是命令不会继续运行:
However, when I run the code below, the browser immediately is free, but the command doesn't continue to run:
exec("/usr/local/php53/bin/php csv.php $file $user > /dev/null &");
当我运行ps auxw
时,我看到一个正在运行的进程在16秒后死亡(并且似乎很快耗尽了内存并占用了大量CPU%):
When I run ps auxw
I see one running process that dies after 16 seconds (and seems to eat up memory quickly and use a lot of CPU%):
/usr/local/php53/bin/php csv.php /tmp/php9Pwu9e 294
然后,该过程将在没有实际执行任何操作的情况下终止.不知道&符在做什么会导致这种情况.
Then the process dies without having actually done anything. Not sure what the ampersand is doing that would cause this.
当末尾没有&"号时,为什么还会出现sh -c
的问题?我认为这可能表示某些事情,但不知道是什么.
Also why does sh -c
appear when no ampersand is present at the end? I feel that this may be indicative of something, but have no idea what.
因为这一直作为答案出现, 我也尝试过:
Because this keeps cropping up as an answer, I have also tried:
exec("/usr/local/php53/bin/php csv.php $file $user > /dev/null 2>&1 &");
这证明了与上述相同的问题. 下面的代码可以运行,但不会释放浏览器.
Which demonstrates the same issue as mentioned above. The code below, does run, but does NOT free up the browser.
exec("usr/local/php53/bin/php csv.php $file $user > /dev/null 2>&1");
推荐答案
问题比我想象的要简单得多.问题是找不到$file
(这是一个存储在/tmp
目录中的上载文件).我不确定为什么,但是我想这与在后台进程获取文件之前将其销毁有关.
The issue was much simpler than I had thought. The problem was the $file
(which was an uploaded file being stored in the /tmp
directory) was not found. I'm not sure why, but I imagine it has something to do with the file being destroyed before the background process can get to it.
解决方案是将文件move_uploaded_file()
固定到一个永久位置,然后使用该文件运行脚本.那行得通.
The solution was to move_uploaded_file()
the file to a permanent location, and run the script with that file. That worked.
这篇关于使用dev> null&运行exec()命令不起作用,但是dev> null起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!