cronjob等待理解

时间:2012-10-16 10:47:55

标签: linux bash shell cron

吼叫是我的cronjob脚本,我只想要

等待 3rd_job 完成 4th,5th

&安培;等待 2nd_job 完成以启动 6,7,8th 作业。

如何提前实现这一目标

目前正在开始 4,5th 工作,它正在等待 1,2,3th 工作完成但我只想等待第3次开始 4,5th 并等待第二次完成以表明 6,7,8th

    #!/bin/bash
    cd /home/ubuntu/
    PATH=$PATH:/usr/local/bin
    export PATH
    nohup scrapy crawl 1st_job &
    nohup scrapy crawl 2nd_job &
    nohup scrapy crawl 3rd_job &
    wait $(pgrep 3rd_job)
    nohup scrapy crawl 4th_job &
    nohup scrapy crawl 5th_job &
    wait $(pgrep 2nd_job)
    nohup scrapy crawl 6th_job &
    nohup scrapy crawl 7th_job &
    nohup scrapy crawl 8th_job &

1 个答案:

答案 0 :(得分:1)

您可以使用包含上次生成的异步任务的PID的$!变量:

#!/bin/bash
cd /home/ubuntu/
PATH=$PATH:/usr/local/bin
export PATH
nohup scrapy crawl 1st_job &
nohup scrapy crawl 2nd_job &
pid_2nd="$!"
nohup scrapy crawl 3rd_job &
wait $!
nohup scrapy crawl 4th_job &
nohup scrapy crawl 5th_job &
wait $pid_2nd
nohup scrapy crawl 6th_job &
nohup scrapy crawl 7th_job &
nohup scrapy crawl 8th_job &

如果你想要一些更复杂的东西,例如以有向无环图方式执行(例如,一旦第3完成开始4-5,并且第二次完成开始6-8)我建议你重新组织调用函数,以便可以异步调用它们:

#!/bin/bash
second () {
    nohup scrapy crawl 2nd_job
    nohup scrapy crawl 6th_job &
    nohup scrapy crawl 7th_job &
    nohup scrapy crawl 8th_job &
}

third () {
    nohup scrapy crawl 3rd_job
    nohup scrapy crawl 4th_job &
    nohup scrapy crawl 5th_job &
}

cd /home/ubuntu/
PATH=$PATH:/usr/local/bin
export PATH
nohup scrapy crawl 1st_job &
third &
second &

希望这有助于=)