繁体   English   中英

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

[英]Run script after wget downloading is complete (wget background mode)

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

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

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

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

齐纳,谢谢你

当您使用带-b选项的wget时,该命令会在其他shell会话( setsid )中创建子进程 启动子进程后,原始进程完成。

子进程在其他shell会话中运行,因此我们不能使用wait命令。 但我们可以编写一个循环来检查子进程是否正在运行。 我们需要subprocess的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);
...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM