write pid file at appropriate time