singleinstance

package module
v0.0.0-...-d099710 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2021 License: MIT Imports: 4 Imported by: 18

README

go-singleinstance

Cross plateform library to have only one instance of a software (based on python's tendo).

Usage

package main

import (
	"fmt"
	"time"

	"github.com/allan-simon/go-singleinstance"
)

func main() {
	lockFile, err := singleinstance.CreateLockFile("plop.lock")
	if err != nil {
		fmt.Println("An instance already exists")
		return
	}
	defer lockFile.Close()

	fmt.Println("Sleeping...")
	time.Sleep(10 * time.Second)
	fmt.Println("Done")
}

If you try to launch it twice, the second instance will fail.

Thanks

For the python library trendo, from which I've shamelessly adapted the code.

Contribution

Don't be afraid if it says "last commit 2 years ago", this library is made to be small and simple so it's unlikely it changes after some times, however I'm pretty reactive on github overall, so feel free to use issues to ask question, propose patch etc. :)

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateLockFile

func CreateLockFile(filename string) (*os.File, error)

CreateLockFile tries to create a file with given name and acquire an exclusive lock on it. If the file already exists AND is still locked, it will fail.

func GetLockFilePid

func GetLockFilePid(filename string) (pid int, err error)

If filename is a lock file, returns the PID of the process locking it

Types

This section is empty.

Jump to

Keyboard shortcuts

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