Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

share|improve this question

3 Answers 3

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.

share|improve this answer

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)

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

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.