write pid file, and lock it to prevent running multiple copies