Write the PID file as late as possible