1

I have gone through several SE posts on the unix wait command and none of the posts resolve my problem. I have a weather forecast model to run. It has first to prepare some folders, then download data using wget, then run the forecast. The data download can take anything up to 30-60 minutes due to low speed. What happens is that when the data download starts the program goes ahead to run commands before the data is ready. Below is my crontab entry to start the run

15 09 * * * /bin/bash /home/zmumba/WRF/SCRIPTS/wrf_run_00.sh &

This is to run at 09 15 everyday, every week and every month.

The files to run in wrf_run_00.sh are

!/bin/bash
year=`date "+%Y"`
month=`date "+%m"`
day=`date "+%d"`
 source /home/zmumba/.bashrc

perl /home/zmumba/WRF/SCRIPTS/wrf_Namelists_and_Dirs_00_Daily.pl
perl /home/zmumba/WRF/SCRIPTS/download-file_gen00.pl

wget --directory-prefix=/home/zmumba/WRF/DATA/${year}${month}${day}00 --background --continue --progress=bar --input-file=/home/zmumba/WRF/SCRIPTS/download-file-list-one_deg_00.txt

 wait$!

cd /home/zmumba/WRF/WPS

and the rest of the commands to run the model follow. Any assistance will be appreciated.

Zilore Mumba
  • 133
  • 1
  • 6

3 Answers3

4

wait$! is a syntax error, you're missing a space. This happens to have no impact here because you never actually start a background job, so $! remains empty and the command is wait with no argument, which waits for all background jobs — and since there are none, it returns immediately.

There's a difference between using wget --background … and wget … &. The first command tells wget to fork a child process and then exit:

(bash)

  fork ——————————————→
  wait                  exec wget --background …
   ⋮
   ⋮                    fork ———————————————————————→
      ←——————————————   exit                           download
  (next script line)                                             ⋮

When the parent wget process (which is a foreground) returns, bash resumes the execution of the process. With wget … &, the shell forks a background process:

(bash)

  fork ——————————————————→
  (next script line)       exec wget --background …

                           download
                            ⋮

In this case, you can wait for the wget process to exit:

wget … &
wait $!

However this is pointless: starting a background job only to wait for it is equivalent to starting a foreground job.

wget …
3

As suggested, you could both try the command

wait
#instead of 
wait$!

because I really doubt that there's any command named wait$! maybe what you want here is using wait $!

or as it's a cron script you could maybe disable the option of wget --background because you want to wait for the end of this command and not put it in the background.

Kiwy
  • 9,534
  • Thanks @Kiwy, simply removing the backgrounding of wget works. Sorry for my not thinking of this in the first place and saving your effort. – Zilore Mumba Apr 02 '14 at 09:57
  • @ZiloreMumba we all make mistakes, me the first most of the time. I'm glad this helped you – Kiwy Apr 02 '14 at 09:59
-1

Have you tried wait{!}? It will wait for all background processes to finish before proceeding.