Try Jon M's suggestions, including wait_for_pid and nested spawn.