actually write (and cleanup) the pidfile