开发者

How to wait for a task to complete before continuing shell script

I have two shell scripts to write that will be executed nightly via cron schedule.

On my production server:

  1. mysqldump -ufoo -pbar --opt --routines db > ~/sqldump/{$todays_date}.sql开发者_开发百科
  2. ln -s ~/sqldump/{$todays_date}.sql latest.sql

On my development server:

  1. scp foo@domain.tld:~/sqldump/latest.sql ~/sqldump
  2. mysql -ufoo -pbar db < latest.sql

I have two questions:

  1. In the production server job, how do i get $todays_date to fill in the date? e.g., "2010-07-21"
  2. How do I make step two wait for step 1 to finish in each job?


To get today's date, use date +%F:

command > ~/sqldump/$(date +%F).sql

To read more about the various options you can use with date's + formatting, check out the date man page.

As for waiting, I assume you mean you don't want to run the command on your dev server until the commands on your prod server are done. The best idea would be to have the dev server start the command using ssh (i.e. ssh user@host "mysqldump ... > ... && ln -s ...).

If you don't want to do that, the best I can think of is to use a temp file so that the dump file creation is atomic:

mysqldump ... > ~/sqldump/tmp$$ && mv ~/sqldump/tmp$$ ~/sqldump/$(date +%F).sql

Then if you have your dev server request's the dump from today's date, then you can loop until the scp succeeds:

while ! scp foo@domain.tld:~/sqldump/$(date +%F).sql ~/sqldump; do
    # file not present yet, sleep 1 minute and try again
    sleep 60
done
mysql -ufoo -pbar db < latest.sql


Unless you use an "&" at the end of the command for step one, step two will automatically wait for it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜