Wait For

I'm currently downloading all the DefCon videos and rather then try to grab them all at once I've broken the list down into batches. I wanted an automated way to start each batch when the previous one had finished so I wrote this little script to wait for something to finish then do something else.

#!/usr/bin/env bash
 
while [ "`ps x|grep -E '[0-9:]+ wget'`" != "" ]; do
        echo "still running"
        sleep 2
done
 
echo "Finished"
echo "Do something else"

This just checks the output from ps looking for time field followed by the command I'm waiting for, if there is no output then the command has finished, if there is then it is still running. I could have added cut into this to only search the fifth column and then greped that but why put an extra command in when it isn't needed.

An alternative way to do this is using the pidof command to get the process ids of the script you are waiting for and checking if that comes back empty, here is that version.

#!/usr/bin/env bash
 
while [ "`pidof wget`" != "" ]; do
        echo "still running"
        sleep 2
done
 
echo "Finished"
echo "Do something else"

Submitted by Robin Wood