问题描述
我正在尝试从PHP执行此操作-基本上是从远程位置下载jpg,并在必要时调整其大小。第二个任务不是那么重要,但是我想要的是让第二个任务等待wget成功完成,但是对于它们两者都是按顺序在后台运行,并且不会像任务那样占用PHP线程对于PHP而言,它并不是立即重要的,而是出于缓存目的:
shell_exec('(wget -q -O /tmp/pic1.jpg https://r.ddmcdn.com/s_f/o_1/APL/uploads/2014/10/nyan-cat-01-625x450.jpg&& convert /tmp/pic1.jpg -quality 80 -interlace Plane -resize 180x\> /tmp/pic1_reduced.jpg) &');
但是上面发生的事情是PHP挂起,直到两个任务都完成(为什么给定&后缀?)
如果我使用wget -b
开关(虽然如果我已经在使用&?,为什么需要这样做),它确实作为后台任务运行,但是第二个任务(convert
)失败,因为wget任务现在位于另一个线程上,它尝试转换尚未(完全)下载的文件。
哦,如果我愿意的话:
(wget -q -O /tmp/pic1.jpg https://r.ddmcdn.com/s_f/o_1/APL/uploads/2014/10/nyan-cat-01-625x450.jpg&& convert /tmp/pic1.jpg -quality 80 -interlace Plane -resize 180x\> /tmp/pic1_reduced.jpg) &
直接在外壳中,它不会绑住终端,而是按照我想要的方式运行-作为两个连续的后台任务。
那么如何从PHP触发此任务序列,使其在shell中作为后台任务顺序运行?
编辑:哦,作为讨厌的黑客,我试图像这样添加sleep 10&&
:
shell_exec('(wget -bq -O /tmp/pic1.jpg https://r.ddmcdn.com/s_f/o_1/APL/uploads/2014/10/nyan-cat-01-625x450.jpg&& sleep 10&& convert /tmp/pic1.jpg -quality 80 -interlace Plane -resize 180x\> /tmp/pic1_reduced.jpg) &');
...发现如果没有在10秒钟之内完成,它基本上还是会失败,我以后会知道它失败了,因为文件不存在。
但是,即使sleep 10&
在后台运行,即使for(i=0; i<1000000000; i++){}
确实在后台运行,也可以将PHP挂起10秒钟。我想我这里缺少有关后台任务如何工作的信息。
解决方法
好的,我想我破解了。您仍然需要像这样将输出定向到/ dev / null:
shell_exec('(wget -q -O /tmp/pic1.jpg https://r.ddmcdn.com/s_f/o_1/APL/uploads/2014/10/nyan-cat-01-625x450.jpg&& convert /tmp/pic1.jpg -quality 80 -interlace Plane -resize 180x\> /tmp/pic1_reduced.jpg) > /dev/null 2>/dev/null &');
这现在似乎产生了我想要的行为,例如,一组劳动密集型的命令,如下所示:
shell_exec('(wget -q -O /tmp/pic1.jpg https://r.ddmcdn.com/s_f/o_1/APL/uploads/2014/10/nyan-cat-01-625x450.jpg&& convert /tmp/pic1.jpg -quality 80 -interlace Plane -resize 180x\> /tmp/pic1_reduced.jpg) > /dev/null 2>/dev/null &');
shell_exec('(wget -q -O /tmp/pic2.jpg https://r.ddmcdn.com/s_f/o_1/APL/uploads/2014/10/nyan-cat-01-625x450.jpg&& convert /tmp/pic2.jpg -quality 80 -interlace Plane -resize 180x\> /tmp/pic2_reduced.jpg) > /dev/null 2>/dev/null &');
shell_exec('(wget -q -O /tmp/pic3.jpg https://r.ddmcdn.com/s_f/o_1/APL/uploads/2014/10/nyan-cat-01-625x450.jpg&& convert /tmp/pic3.jpg -quality 80 -interlace Plane -resize 180x\> /tmp/pic3_reduced.jpg) > /dev/null 2>/dev/null &');
shell_exec('(wget -q -O /tmp/pic4.jpg https://r.ddmcdn.com/s_f/o_1/APL/uploads/2014/10/nyan-cat-01-625x450.jpg&& convert /tmp/pic4.jpg -quality 80 -interlace Plane -resize 180x\> /tmp/pic4_reduced.jpg) > /dev/null 2>/dev/null &');
shell_exec('(wget -q -O /tmp/pic5.jpg https://r.ddmcdn.com/s_f/o_1/APL/uploads/2014/10/nyan-cat-01-625x450.jpg&& convert /tmp/pic5.jpg -quality 80 -interlace Plane -resize 180x\> /tmp/pic5_reduced.jpg) > /dev/null 2>/dev/null &');
shell_exec('(wget -q -O /tmp/pic6.jpg https://r.ddmcdn.com/s_f/o_1/APL/uploads/2014/10/nyan-cat-01-625x450.jpg&& convert /tmp/pic6.jpg -quality 80 -interlace Plane -resize 180x\> /tmp/pic6_reduced.jpg) > /dev/null 2>/dev/null &');
shell_exec('(wget -q -O /tmp/pic7.jpg https://r.ddmcdn.com/s_f/o_1/APL/uploads/2014/10/nyan-cat-01-625x450.jpg&& convert /tmp/pic7.jpg -quality 80 -interlace Plane -resize 180x\> /tmp/pic7_reduced.jpg) > /dev/null 2>/dev/null &');
...不捆绑PHP,但在/ tmp文件夹中产生以下输出一段时间后立即返回: