Since the script given by Josh Arenberg might have some deadlocking issues (which I did not experience so far, but also have not investigated), I have written up something on my own. It should not have deadlocking problems. It also works for any shell command, not just cp.
Contents of ~/bin/q
#!/bin/bash
#this waits for any PIDs to finish
anywait(){
for pid in "$@"; do
while kill -0 "$pid" 2&>1 >/dev/null; do
sleep 0.5
done
done
}
PIDFILE=~/.q.pid
#open PIDFILE and aquire lock
exec 9>>$PIDFILE
flock -w2 9 || { echo "ERROR: flock() failed." >&2; exit 1; }
#read previous instances PID from PIDFILE and write own PID to PIDFILE
OLDPID=$(<$PIDFILE)
echo $$>$PIDFILE
#release lock
flock -u 9
#wait for OLDPID
anywait $OLDPID
#do stuff
"$@"
#afterwards: cleanup (if pidfile still contains own PID, truncate it)
flock -w2 9 || { echo "ERROR: flock() failed." >&2; exit 1; }
if [ $(<$PIDFILE) == $$ ]; then
truncate -s0 $PIDFILE
fi
flock -u 9
It creates a chain of processes, each waiting for the previous one. If a process in the middle of the chain crashes while waiting (unlikely but not impossible), the chain is broken and both parts run in parallel. The same happens if one of the processes is killed.
Usage like this:
q $COMMAND $ARGS
or even
q $COMMAND $ARGS; $ANOTHER_COMMAND $MORE_ARGS
Test e.g. by typing
q sleep 10 &
q echo blubb &
and finding that after 10 seconds blubb is printed.