I have to chmod and chown hundreds of thousands of files as part of a migration script. Each command takes about an hour and a half to complete. I realized these two operations can be run at the same time which cuts down on running time, which I confirmed by testing in the shell.

I know the trick of pushing commands into the background with '&', but I need to make sure both processes finish before proceeding with the rest of the script.

Thanks

link|improve this question

72% accept rate
feedback

3 Answers

Use the wait command.

This demo:

#!/bin/bash
echo $SECONDS
sleep 12&
sleep 15&
jobs
wait
echo $SECONDS
echo "jobs are done"

Produces this output:

0
[1]-  Running                 sleep 12 &
[2]+  Running                 sleep 15 &
15
jobs are done

There's a fifteen second pause before the last two lines are output.

link|improve this answer
feedback

You can capture the pid and use wait.

chmod options
CHMODPID=$!
chown options
CHOWNPID=$!
wait $CHMODPID
wait $CHOWNPID

(should work with bash; details may differ on other shell types)

link|improve this answer
feedback

If you need to make sure that both processes are finished before proceeding to the next step, a simple solution would be to write something to a file and check it. Just do a head or cat on the file (with default value 0) and add 1 to the first line. If you detect 2, then both processes are completed. Of course the addition should be done at the very bottom of your scripts.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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