【问题标题】:shell script on linuxlinux上的shell脚本
【发布时间】:2012-11-03 10:30:21
【问题描述】:

这是我的 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 &

我想运行什么first add_columns.py 脚本

然后爬虫脚本(爬虫中的所有脚本都是异步的

终于想运行final_script.py

但是使用上面的shell脚本

final_script.py 在结束前执行

nohup scrapy crawl l &
nohup scrapy crawl m &

虽然我在爬虫上等待

crawlers &
wait $!

最后,我如何才能在 crawlers() 方法中的所有作业完成后才能调用 final_script.py。

谢谢

【问题讨论】:

  • 从除“scrapy crawl”之外的所有调用中删除“&”,删除所有对“wait”的调用,添加“for pid in $(jobs -p); do wait $pid || exit $ ?; done" 到函数 "crawlers" 结束

标签: linux shell cron


【解决方案1】:

首先,为什么要麻烦将您立即wait 的事情放在后台?

其次,在crawlers 函数中,您只在一半的调用中使用waiting;另一半可能仍在运行。

使用不带参数的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

【讨论】:

  • 感谢您的贡献,我的爬虫功能我需要等待半个进程结束才能下一次开始,是的,我还需要在爬虫结束时添加等待。但为什么它只在爬虫功能中起作用?在爬虫调用后等待不应该等待结束所有以爬虫结尾的子进程?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-05
  • 2012-05-12
  • 2014-10-09
  • 2011-05-07
  • 2014-03-26
相关资源
最近更新 更多