【问题标题】:Run script after wget downloading is complete (wget background mode)wget下载完成后运行脚本(wget后台模式)
【发布时间】:2016-08-26 00:14:17
【问题描述】:

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

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

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

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

谢谢你,齐纳

【问题讨论】:

  • 只需创建一个脚本文件并将这两个命令(不带 -b)添加到其中,然后执行脚本,以便按顺序加载。你甚至可以让它有参数,这样你就可以做类似bash script.sh website.com

标签: php linux wget download


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-30
    • 1970-01-01
    • 2023-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-02
    相关资源
    最近更新 更多