Detach betwen opening pid file and writing to it

This resolves the initial problem of not getting errors when the
service manager fails to start because of a failure opening the pid
file. It also puts the correct pid into the file, which is what the
file is for in the first place.