douben8492 2016-08-26 00:14
浏览 1364

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

I would like to run a PHP script after wget finished downloading. That's no problem and I can use someting like...

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

BUT! I use background downloading of wget (wget -b) which returns something like Continuing in background, pid 12345.

Is possible to run wget on background and run script after download?

Thank you, zener

  • 写回答

1条回答 默认 最新

  • doukezi4606 2016-08-27 04:19
    关注

    When you use wget with -b option the command create a subprocess in other shell session (setsid). The original process finishes after the subprocess was launched.

    The subprocess runs in other shell session so we can't use wait command. But we can write one loop that check if subprocess is running. We need subprocess's pid.

    By example:

    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 &
    

    Another way to get the pid of the subprocess could be parse the output of wget.

    Aditionally, for understand how work background option of wget you can see the source code in C:

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

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示