我正试着从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) &直接在shell中,它不会占用终端,但看起来就像我想要的那样运行--作为两个连续的后台任务。
那么,如何从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) &');...figuring说,如果在10秒内没有完成,那么它基本上是失败的,我稍后就会知道它失败了,因为文件不在那里。
但显然这会使PHP挂起10秒钟,即使sleep 10&确实在后台运行,如果您在shell中尝试它的话。我想我在这里遗漏了一些关于后台任务如何工作的东西。
发布于 2020-09-03 17:25:25
好了,我想我破解了。您仍然需要将输出定向到/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 &');这似乎产生了我想要的行为,.e.g一组像这样的劳动密集型命令:
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 &');...does不会占用PHP,但会在一段时间后立即返回,并在/tmp文件夹中产生以下输出:
https://stackoverflow.com/questions/63719978
复制相似问题