2012-11-03 199 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

感谢您的贡献我的爬虫函数我需要等待一半的进程才能在下一次开始之前结束,是的,我需要在爬虫末尾添加等待以及。 但为什么它只能在爬虫功能中工作?在抓取程序调用是否应该等待结束在抓取程序中结束的所有子进程之后等待? –