wget作为后台任务,然后是连续的后台任务



我正试图从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) &');

我想,如果10秒内没有完成,基本上就是失败了,我以后会知道它失败了,因为文件不在那里。

但很明显,这会使PHP挂起10秒,即使如果您在shell中尝试sleep 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文件夹中生成以下输出一段时间后立即返回:

/tmp文件夹的屏幕截图

最新更新