Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have 3 nice commands that are dumping some databases, and compress assets for a backup. After that, the files should be copied to another server. I'm using this command:

nice mysqldump -u user1 -p'XXX' database1 | gzip > database1.sql.gz
nice mysqldump -u user2 -p'XXX' database2 | gzip > database2.sql.gz
nice zip -r assets.zip /var/www/site1/assets/

cd ..

scp -R backups user@backupserver.com:/home/user/backups

My problem is, that the scp commands seems to get executed, BEFORE the nice commands are finished. This leads to partial SQL dumps, and sometimes broken zip files.

So how can I be sure, that all 3 nice commands are finished, before copying the files to another server?

share|improve this question
Is this all inside of a bash script (somescript.bash), and none of these are backgrounded, correct? If so I don't see how the next command could start until the prior one was done. –  slm Jul 23 '13 at 2:42
There is probably another reason your mysqldump is incomplete (eg a corrupt table). –  jordanm Jul 23 '13 at 2:51

1 Answer 1

You want to use the wait shell built in.

Execute commands in the background, then tell the shell to wait for all of them to finish before continuing. Here's a very basic example:


sleep 6 && echo first sleep  &
sleep 3 && echo second sleep &

echo "waiting"
echo "done waiting"


  • The pid of background processes is collected into WAITPID and the list is fed to wait. You can also use job numbers instead.
  • When you run this you'll see that second sleep finishes first.
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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