gpt4 book ai didi

php - wget下载完成后运行脚本(wget后台模式)

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:23:33 25 4
gpt4 key购买 nike

我想在 wget 下载完成后运行一个 PHP 脚本。这没问题,我可以使用类似...

wget http://example.com && php script.php

但是!我使用 wget (wget -b) 的后台下载,它返回类似 Continuing in background, pid 12345 的内容。

是否可以在后台运行 wget 并在下载后运行脚本?

谢谢你,齐纳

最佳答案

当您将 wget 与 -b 选项一起使用时,该命令会在其他 shell session (setsid) 中创建一个子进程。启动子流程后,原始流程结束。

子进程在其他 shell session 中运行,因此我们不能使用 wait 命令。但是我们可以编写一个循环来检查子进程是否正在运行。我们需要子进程的 pid。

举例:

wget -b http://example.com && \
(
PID=`pidof wget | rev | cut -f1 | rev`;
while kill -0 $PID 2> /dev/null; do sleep 1; done;
) && \
php script.php &

获取子进程 pid 的另一种方法是解析 wget 的输出。

此外,要了解wget的后台选项如何工作,您可以查看C中的源代码:

...
pid = fork ();
if (pid < 0)
{
perror ("fork");
exit (1);
}
else if (pid != 0)
{
printf (_("Continuing in background, pid %d.\n"), (int)pid);
if (logfile_changed)
printf (_("Output will be written to `%s'.\n"), opt.lfilename);
exit (0);
}
setsid ();
freopen ("/dev/null", "r", stdin);
freopen ("/dev/null", "w", stdout);
freopen ("/dev/null", "w", stderr);
...

关于php - wget下载完成后运行脚本(wget后台模式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39156583/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com