2012-11-03 197 views
2

這裏是我的shell腳本在Linux shell腳本

#!/bin/bash 

crawlers(){ 
    nohup scrapy crawl a & 
    nohup scrapy crawl b & 
    wait $! 
    nohup scrapy crawl f & 
    nohup scrapy crawl g & 
    wait $! 
    nohup scrapy crawl h & 
    nohup scrapy crawl i & 
    wait $! 
    nohup scrapy crawl i & 
    nohup scrapy crawl j & 
    nohup scrapy crawl k & 
    wait $! 
    nohup scrapy crawl l & 
    nohup scrapy crawl m & 
} 

PATH=$PATH:/usr/local/bin 
export PATH 

python add_columns.py & 
wait $! 
crawlers & 
wait $! 
python final_script.py & 

什麼我想運行第一 add_columns.py腳本

然後爬蟲腳本(在爬蟲的所有腳本都是異步

終於想跑final_script.py

但使用上面的shell腳本

final_script.py已經結束

nohup scrapy crawl l & 
nohup scrapy crawl m & 

之前執行的,雖然我把等待cralwers

crawlers & 
wait $! 

最後,我怎麼能achive調用final_script.py只有在crawlers()方法完成所有作業之後。

感謝

+0

除「scrapy crawl」之外的所有調用中刪除「&」,刪除所有調用「等待」,添加「爲pid $(jobs -p);等待$ pid || exit $ ?; done「到函數」crawlers「的末尾 – bobah

回答

0

首先,爲什麼還要backgrounding事情,你馬上wait呢?

二,crawlers函數中,你只有wait ing的一半呼叫;另一半可能仍在運行。

使用wait無參數等待所有當前活動的子項退出。這將是一個更好的版本:

#!/bin/bash 

crawlers(){ 
    nohup scrapy crawl a & 
    nohup scrapy crawl b & 
    nohup scrapy crawl f & 
    nohup scrapy crawl g & 
    nohup scrapy crawl h & 
    nohup scrapy crawl i & 
    nohup scrapy crawl i & 
    nohup scrapy crawl j & 
    nohup scrapy crawl k & 
    nohup scrapy crawl l & 
    nohup scrapy crawl m & 

    wait 
} 

PATH=$PATH:/usr/local/bin 
export PATH 

python add_columns.py 

crawlers 

python final_script.py 
+0

感謝您的貢獻我的爬蟲函數我需要等待一半的進程才能在下一次開始之前結束,是的,我需要在爬蟲末尾添加等待以及。 但爲什麼它只能在爬蟲功能中工作?在抓取程序調用是否應該等待結束在抓取程序中結束的所有子進程之後等待? –