Display exception when writing pid file.
Fix pid locking.