pidfile

package module
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2025 License: MIT Imports: 9 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 file that contains a decimal number also known as the PID (short for process identifier).

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.
  • Passes the command line arguments of any next instance to the first instance.

Installation

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

go get github.com/mevdschee/pidfile

Example

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

package main

import (
	"log"
	"os"

	"github.com/mevdschee/pidfile"
)

func main() {

	// create pidfile struct based on identifier
	pf := pidfile.New("app_identifier")
	// when a second instance is started
	pf.OnSecond = func(args []string) {
		log.Printf("another instance was started")
	}
	// create pidfile on application start
	err := pf.Create()
	if err != nil {
		log.Fatalf("can't create pidfile: %v", err)
	}
	// remove pidfile on application close
	defer pf.Remove()
	// if this is not the first instance, then close it
	if pf.FirstPid != os.Getpid() {
		return
	}

	// application code
}

NB: This package was built for usage in a (desktop) Fyne project.

Future work / Known issues

The following issues need addressing in future versions:

  • Add parallel tests to validate starting once does not have race conditions
  • Add parallel tests to validate arguments lock prevents race conditions

The following packages have inspired me to make this package:

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Pidfile

type Pidfile struct {
	Signal   syscall.Signal
	AppId    string
	FullPath string
	FirstPid int
	OnSecond func([]string)
}

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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