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

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);
...