Explorar o código

write pidfile

Tobias Begalke %!s(int64=7) %!d(string=hai) anos
pai
achega
2e108be605
Modificáronse 1 ficheiros con 8 adicións e 1 borrados
  1. 8 1
      main.go

+ 8 - 1
main.go

@@ -21,6 +21,7 @@ import (
 
 	"git.scraperwall.com/scw/data"
 	"git.scraperwall.com/scw/ip"
+	"git.scraperwall.com/scw/pidfile"
 )
 
 var (
@@ -34,6 +35,7 @@ var (
 	sleepFor              = flag.Duration("sleep", 0, "Sleep this long between sending data (only when replaying a file)")
 	requestsFile          = flag.String("requests", "", "CSV file containing requests (IP and URL)")
 	useXForwardedAsSource = flag.Bool("use-x-forwarded", false, "Use the IP address in X-Forwarded-For as source")
+	pidFile               = flag.String("pidfile", "/var/run/munchclient.pid", "The location of the PID file")
 	doVersion             = flag.Bool("version", false, "Show version information")
 
 	natsEC  *nats.EncodedConn
@@ -55,12 +57,17 @@ func init() {
 }
 
 func main() {
-
 	if *doVersion {
 		version()
 		os.Exit(0)
 	}
 
+	if err := pidfile.Write(*pidFile); err != nil {
+		log.Fatal("munchclient is already running, exiting!")
+		os.Exit(1)
+	}
+	defer pidfile.Remove(*pidFile)
+
 	go func(c *uint64) {
 		for {
 			fmt.Printf("%d requests per second\n", *c)