pidfile

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2025 License: MIT Imports: 6 Imported by: 1

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 a PID (Process ID) file.

Features

  • Detects that the an instance of the application is already running.
  • Allows you to prevents multiple instances of the application from running concurrently.
  • Signals the first instance of the application when a next instance is started.

Installation

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

go get github.com/mevdschee/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/mevdschee/pidfile"
)

func main() {

    // create PID file struct based on identifier
    pf := pidfile.New("app_identifier")
    // create PID file on application start
    err := pf.Create();
    if err != nil {
        log.Fatalf("can't create PID file: %v", err)
    }
    // remove PID file on application close
    defer pf.Remove();
    // if this is not the first instance, then close it
    if pf.FirstPid != os.Getpid() {
        return
    } 
    
    // application code
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Pidfile

type Pidfile struct {
	FullPath string
	FirstPid int
	OnSecond func()
}

Pidfile represents the name

func New

func New(appId string) *Pidfile

New creates a Pidfile instance based on the application ID

func (*Pidfile) Create

func (pf *Pidfile) Create() error

Create creates a pidfile

func (*Pidfile) Remove

func (pf *Pidfile) Remove() error

Remove removes a pidfile

Jump to

Keyboard shortcuts

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