pidfile

package
v0.0.93-latest-master Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 6, 2026 License: MIT, MIT Imports: 9 Imported by: 0

README

PIDFile Package for Go

The pidfile package provides a simple way to ensure that only one instance of a Go application runs at any given time by using PID (Process ID) files.

Features

  • Easy creation and management of a PID file.
  • Checks if an application is already running.
  • Prevents multiple instances of the application from running concurrently.

Installation

To install the pidfile package, use the following go get command:

go get github.com/makifdb/pidfile

Example

Here is a full example of an application that uses the pidfile package to ensure single instance execution

package main

import (
    "fmt"
    "log"
    "os"
    "github.com/makifdb/pidfile"
)

func main() {
    // Define the PID file path
    pidFilePath := filepath.Join(os.TempDir(), "example.pid")

    // Create or update the PID file
    err := pidfile.CreateOrUpdatePIDFile(pidFilePath)
    if err != nil {
        log.Fatalf("Unable to create or update PID file: %v", err)
    }
    
    // Defer the removal of the PID file on application exit
    defer os.Remove(pidFilePath)
    
    // Your application logic goes here
    fmt.Println("Application started successfully.")

    // Application logic...
}

Notes

This package assumes that the PID file is stored in a location that is writable by the application. Make sure to handle the PID file correctly to prevent orphaned PID files, which could prevent the application from starting.

License

This pidfile package is open-source software licensed under the MIT license.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrPIDExists  = errors.New("PID file already exists with an active process")
	ErrLockFailed = errors.New("failed to acquire a lock on the PID file")
	ErrPIDStolen  = errors.New("PID file has been stolen by another process")
)

Functions

This section is empty.

Types

type PIDFile

type PIDFile struct {
	// contains filtered or unexported fields
}

func CreateOrUpdatePIDFile

func CreateOrUpdatePIDFile(filename string) (*PIDFile, error)

CreateOrUpdatePIDFile ensures that a PID file exists and contains the current process's PID. It attempts to create the PID file if it does not exist, and update it if the process is not active.

func CreateUpdateOrRetryPIDFile

func CreateUpdateOrRetryPIDFile(ctx context.Context, filename string, timeout time.Duration) (*PIDFile, error)

func (*PIDFile) Close

func (pidfile *PIDFile) Close() error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL