rename
This commit is contained in:
50
daemon.go
Normal file
50
daemon.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"runtime"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func getPidFile() string {
|
||||
return projectFolder + "/.fileboy.pid"
|
||||
}
|
||||
|
||||
func runAsDaemon() (int, error) {
|
||||
if runtime.GOOS == "windows" {
|
||||
logAndExit("daemons mode cannot run on windows.")
|
||||
}
|
||||
err := stopDaemon()
|
||||
if err != nil {
|
||||
logAndExit(err)
|
||||
}
|
||||
_, err = exec.LookPath("fileboy")
|
||||
if err != nil {
|
||||
logAndExit("cannot found `fileboy` command in the PATH")
|
||||
}
|
||||
daemon := exec.Command("fileboy")
|
||||
daemon.Dir = projectFolder
|
||||
daemon.Env = os.Environ()
|
||||
daemon.Stdout = os.Stdout
|
||||
err = daemon.Start()
|
||||
if err != nil {
|
||||
logAndExit(err)
|
||||
}
|
||||
pid := daemon.Process.Pid
|
||||
if pid != 0 {
|
||||
ioutil.WriteFile(getPidFile(), []byte(strconv.Itoa(pid)), 0644)
|
||||
}
|
||||
return pid, nil
|
||||
}
|
||||
|
||||
func stopDaemon() error {
|
||||
bs, err := ioutil.ReadFile(getPidFile())
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
_ = exec.Command("kill", string(bs)).Run()
|
||||
os.Remove(getPidFile())
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user