The relevant part of /proc/mdstat looks like below, where U means a device that is "up", and _ a device that is "down". When it's recovering, the devices show as "down", so we could wait until all devices are "up" again.
md0 : active raid5 sda1[0] sdd1[2] sdb1[1]
1465151808 blocks level 5, 64k chunk, algorithm 2 [4/3] [UUU_]
So, something like this:
dev=md0
until grep -A1 "^$dev :" /proc/mdstat | grep -q '\[UU*\]' ; do
sleep 2
done
run some command here...
(The first grep looks for the given device name, and grep -A1 prints the matching line plus one after. The second looks for a string of Us between the brackets.)
Of course this never checks if a recovery was running in the first place, we'd need smarter logic to interpret the complete output since the number of output lines varies.
mdadm --wait /dev/mdX; command