luet/vendor/github.com/marcsauter/single
Ettore Di Giacinto a674c6515c
update vendor/
2019-11-25 20:32:33 +01:00
..
LICENSE update vendor/ 2019-11-25 20:32:33 +01:00
README.md update vendor/ 2019-11-25 20:32:33 +01:00
single_bsd.go update vendor/ 2019-11-25 20:32:33 +01:00
single_darwin.go update vendor/ 2019-11-25 20:32:33 +01:00
single_linux.go update vendor/ 2019-11-25 20:32:33 +01:00
single_solaris.go update vendor/ 2019-11-25 20:32:33 +01:00
single_unix.go update vendor/ 2019-11-25 20:32:33 +01:00
single_windows.go update vendor/ 2019-11-25 20:32:33 +01:00
single.go update vendor/ 2019-11-25 20:32:33 +01:00

single

single provides a mechanism to ensure, that only one instance of a program is running.

package main

import (
    "log"
    "time"

    "github.com/marcsauter/single"
)

func main() {
    s := single.New("your-app-name")
    if err := s.CheckLock(); err != nil && err == single.ErrAlreadyRunning {
        log.Fatal("another instance of the app is already running, exiting")
    } else if err != nil {
        // Another error occurred, might be worth handling it as well
        log.Fatalf("failed to acquire exclusive app lock: %v", err)
    }
    defer s.TryUnlock()

    log.Println("working")
    time.Sleep(60 * time.Second)
    log.Println("finished")
}

The package currently supports linux, solaris and windows.