簡體   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