Documentation ¶
Overview ¶
Package pidfile provides a way to manage your PID file.
PID file is a file (usually with .pid file extension) with your running program's PID as its content. It is used for both avoiding running multiple same instances and managing the current running instance/service (sending signals).
Example ¶
The most simple usage is to call Write on startup and Remove on shutdown.
// In 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")
Output:
Example (GetRunningPIDValid) ¶
Use GetRunningPIDValid to read a PID file and verifies it is valid (process is running and has the same executable name with current process)
pid, err := pidfile.GetRunningPIDValid("/run/your_instance_name.pid") if err == nil { fmt.Printf("Found an existing instance/service with pid: %d\n", pid) }
Output:
Example (UseStruct) ¶
To avoid writing pidfile's path every time, use PIDFile struct.
// In func main mypidfile := pidfile.PIDFile{Filepath: "/run/your_instance_name.pid"} if err := mypidfile.Write(); 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 mypidfile.Remove()
Output:
Index ¶
- Variables
- func GetRunningPID(filepath string) (int, error)
- func GetRunningPIDValid(filepath string) (int, error)
- func Remove(filepath string) error
- func RemoveForce(filepath string) error
- func SendInterrupt(pid int) error
- func Write(filepath string) error
- func WriteForce(filepath string) error
- type PIDFile
Examples ¶
Constants ¶
This section is empty.
Variables ¶
Functions ¶
func GetRunningPID ¶
GetRunningPID constructs a PIDFile and calls its GetRunningPID. See PIDFile.GetRunningPID for details.
func GetRunningPIDValid ¶
GetRunningPIDValid constructs a PIDFile and calls its GetRunningPIDValid. See PIDFile.GetRunningPIDValid for details.
func RemoveForce ¶
RemoveForce constructs a PIDFile and calls its RemoveForce. See PIDFile.RemoveForce for details.
func SendInterrupt ¶
SendInterrupt sends a Ctrl-C event (SIGINT) to the specified process.
It supports both unix and windows.
func WriteForce ¶
WriteForce constructs a PIDFile and calls its WriteForce. See PIDFile.WriteForce for details.
Types ¶
type PIDFile ¶
type PIDFile struct {
Filepath string
}
PIDFile holds a file path for further related operations.
func (PIDFile) GetRunningPID ¶
GetRunningPID reads PID from file and verifies it indicates a running process.
On success, it returns the pid of running process. Otherwise, PID 0 and error is returned.
func (PIDFile) GetRunningPIDValid ¶
GetRunningPIDValid acts like GetRunningPID and additionally verifies it has the same executable name with the current process.
func (PIDFile) Remove ¶
Remove removes the PID file if GetRunningPIDValid returns PID of current process.
func (PIDFile) RemoveForce ¶
RemoveForce removes the PID file. This is almost the same with os.Remove except it returns nil if the PID file does not exist.
func (PIDFile) Write ¶
Write writes PID of current process to the file if GetRunningPIDValid returns PID 0.
func (PIDFile) WriteForce ¶
WriteForce writes the PID file with PID of current process.