-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
87 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
package util | ||
package pidfile | ||
|
||
// ExistsPid check if pid exists | ||
// ExistsPid checks if pid exists | ||
func ExistsPid(pid int) bool { | ||
return existsPid(pid) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package util | ||
package pidfile | ||
|
||
import ( | ||
"fmt" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
// +build linux freebsd darwin netbsd | ||
|
||
package util | ||
package pidfile | ||
|
||
import ( | ||
"math" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
// +build linux freebsd netbsd | ||
|
||
package util | ||
package pidfile | ||
|
||
import ( | ||
"fmt" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package pidfile | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"strconv" | ||
|
||
"github.com/mackerelio/mackerel-agent/logging" | ||
) | ||
|
||
var logger = logging.GetLogger("pidfile") | ||
|
||
// Create pidfile | ||
func Create(pidfile string) error { | ||
if pidfile == "" { | ||
return nil | ||
} | ||
if pidString, err := ioutil.ReadFile(pidfile); err == nil { | ||
if pid, err := strconv.Atoi(string(pidString)); err == nil { | ||
if ExistsPid(pid) { | ||
return fmt.Errorf("pidfile found, try stopping another running mackerel-agent or delete %s", pidfile) | ||
} | ||
// Note mackerel-agent in windows can't remove pidfile during stoping the service | ||
logger.Warningf("Pidfile found, but there seems no another process of mackerel-agent. Ignoring %s", pidfile) | ||
} else { | ||
logger.Warningf("Malformed pidfile found. Ignoring %s", pidfile) | ||
} | ||
} | ||
|
||
err := os.MkdirAll(filepath.Dir(pidfile), 0755) | ||
if err != nil { | ||
return err | ||
} | ||
file, err := os.Create(pidfile) | ||
if err != nil { | ||
return err | ||
} | ||
defer file.Close() | ||
|
||
_, err = fmt.Fprintf(file, "%d", os.Getpid()) | ||
return err | ||
} | ||
|
||
// Remove pidfile | ||
func Remove(pidfile string) { | ||
if pidfile == "" { | ||
return | ||
} | ||
if err := os.Remove(pidfile); err != nil { | ||
logger.Errorf("Failed to remove the pidfile: %s: %s", pidfile, err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters