Table of Contents
This is a go package to manage your instance/service’s PID file.
See https://unix.stackexchange.com/questions/12815/what-are-pid-and-lock-files-for for what a PID file is been used for.
func main() {
if err := pidfile.Write("/run/your_instance_name.pid"); err != nil {
if errors.Is(err, pidfile.ErrPIDFileInUse) {
fmt.Printf("Instance/Service is already running. Exiting.")
os.Exit(1)
}
panic(fmt.Sprintf("Failed to create PIDFile: %s\n", err.Error()))
}
defer pidfile.Remove("/run/your_instance_name.pid")
}
Once I have a daemon running in system without registering to any system service manager (run by some scripts during boot).
To add a self-update feature, I need to know the old running PID and gracefully kill it before running the updated one.
Searched and didn’t find a package having something like GetPID() which does both reading the PID file and verifies it is running the desired process.