File: lock_example.go

package info (click to toggle)
golang-github-allan-simon-go-singleinstance 1.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 92 kB
  • sloc: makefile: 2
file content (32 lines) | stat: -rw-r--r-- 512 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// +build ignore

package main

import (
	"fmt"
	"time"

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

func main() {
	filename := "plop.lock"

	_, err := singleinstance.CreateLockFile(filename)
	if err != nil {
		fmt.Println("An instance already exists")

		pid, err := singleinstance.GetLockFilePid(filename)
		if err != nil {
			fmt.Println("Cannot get PID:", err)
			return
		}

		fmt.Println("Locking PID:", pid)
		return
	}

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