Coder Social home page Coder Social logo

single's Introduction

single

Build Status Go Report Card GoDoc Go project version MIT Licensed

Ensures that only one instance of the executable is running

Install

go get github.com/crgimenes/single

Examples

package main

import (
	"fmt"
	"os"
	"os/signal"
	"syscall"
	"time"

	"github.com/crgimenes/single"
)

const lockfile = "signal.pid"

func main() {
	err := single.Start(lockfile)
	if err != nil {
		if os.IsExist(err) {
			fmt.Println("another instance of the executable is already running")
			os.Exit(1)
		}
		fmt.Println(err)
		os.Exit(1)
	}
	go func() {
		sc := make(chan os.Signal, 1)
		signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM)
		<-sc
		errc := single.Stop(lockfile)
		if errc != nil {
			fmt.Println(errc)
			os.Exit(1)
		}
		fmt.Println("\n\nhave a nice day!")
		os.Exit(0)
	}()

	fmt.Println("Press ^C to terminate")

	for {
		time.Sleep(1 * time.Second)
		fmt.Print(".")
	}
}

For a process that does not run indefinitely, besides intercepting the signals use defer in the main function to ensure that street completion routine will always be executed

package main

import (
	"fmt"
	"os"
	"os/signal"
	"syscall"
	"time"

	"github.com/crgimenes/single"
)

const lockfile = "signal.pid"

func shutdown() {
	err := single.Stop(lockfile)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	fmt.Println("\n\nhave a nice day!")
	os.Exit(0)
}

func main() {
	err := single.Start(lockfile)
	defer shutdown()
	if err != nil {
		if os.IsExist(err) {
			fmt.Println("another instance of the executable is already running")
			os.Exit(1)
		}
		fmt.Println(err)
		os.Exit(1)
	}
	go func() {
		sc := make(chan os.Signal, 1)
		signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM)
		<-sc
		shutdown()
	}()

	fmt.Println("Processing...")
	time.Sleep(3 * time.Second)
}

single's People

Contributors

crgimenes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

portalgun-io

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.