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

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


cd /home/zmumba/WRF/WPS

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

  • 1
    wait$! would throw an error! – devnull Apr 2 '14 at 7:46
  • @devnull what can I use in place of wait$! – Zilore Mumba Apr 2 '14 at 7:52
  • A simple wait would work. – devnull Apr 2 '14 at 7:53
  • or maybe the option --background shouldn't be used – Kiwy Apr 2 '14 at 7:57

As suggested, you could both try the command

#instead of 

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.

  • 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 2 '14 at 9:57
  • @ZiloreMumba we all make mistakes, me the first most of the time. I'm glad this helped you – Kiwy Apr 2 '14 at 9:59

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:


  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:


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


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 …

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.