From dfb4038654af897d2213d4d306cba1bf2fae914e Mon Sep 17 00:00:00 2001 From: Nick Irvine Date: Fri, 3 Feb 2017 02:02:19 -0800 Subject: [PATCH] Remove pidfile if pidfile was created (#2358) Also, ensure pidfile perms are 644 --- cmd/telegraf/telegraf.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/cmd/telegraf/telegraf.go b/cmd/telegraf/telegraf.go index a3631d38aaf5c..16f7845d0507a 100644 --- a/cmd/telegraf/telegraf.go +++ b/cmd/telegraf/telegraf.go @@ -194,14 +194,21 @@ func reloadLoop( log.Printf("I! Tags enabled: %s", c.ListTags()) if *fPidfile != "" { - f, err := os.Create(*fPidfile) + f, err := os.OpenFile(*fPidfile, os.O_CREATE|os.O_WRONLY, 0644) if err != nil { - log.Fatalf("E! Unable to create pidfile: %s", err) + log.Printf("E! Unable to create pidfile: %s", err) + } else { + fmt.Fprintf(f, "%d\n", os.Getpid()) + + f.Close() + + defer func() { + err := os.Remove(*fPidfile) + if err != nil { + log.Printf("E! Unable to remove pidfile: %s", err) + } + }() } - - fmt.Fprintf(f, "%d\n", os.Getpid()) - - f.Close() } ag.Run(shutdown)