2019-02-09 09:07:47 +00:00
|
|
|
package daemon
|
2019-02-02 16:54:43 +00:00
|
|
|
|
|
|
|
import (
|
2019-02-09 09:07:47 +00:00
|
|
|
"github.com/cnlh/nps/lib/common"
|
2019-02-02 16:54:43 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2019-02-05 16:35:23 +00:00
|
|
|
"path/filepath"
|
2019-02-02 16:54:43 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2019-02-09 09:07:47 +00:00
|
|
|
func InitDaemon(f string, runPath string, pidPath string) {
|
2019-02-02 16:54:43 +00:00
|
|
|
if len(os.Args) < 2 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var args []string
|
|
|
|
args = append(args, os.Args[0])
|
|
|
|
if len(os.Args) >= 2 {
|
|
|
|
args = append(args, os.Args[2:]...)
|
|
|
|
}
|
|
|
|
args = append(args, "-log=file")
|
|
|
|
switch os.Args[1] {
|
|
|
|
case "start":
|
2019-02-09 09:07:47 +00:00
|
|
|
start(args, f, pidPath, runPath)
|
2019-02-02 16:54:43 +00:00
|
|
|
case "stop":
|
2019-02-09 09:07:47 +00:00
|
|
|
stop(f, args[0], pidPath)
|
2019-02-02 16:54:43 +00:00
|
|
|
case "restart":
|
2019-02-09 09:07:47 +00:00
|
|
|
stop(f, args[0], pidPath)
|
|
|
|
start(args, f, pidPath, runPath)
|
2019-02-05 16:35:23 +00:00
|
|
|
case "status":
|
2019-02-09 09:07:47 +00:00
|
|
|
if status(f, pidPath) {
|
2019-02-05 16:35:23 +00:00
|
|
|
log.Printf("%s is running", f)
|
|
|
|
} else {
|
|
|
|
log.Printf("%s is not running", f)
|
|
|
|
}
|
2019-03-26 07:23:39 +00:00
|
|
|
case "reload":
|
|
|
|
reload(f, pidPath)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func reload(f string, pidPath string) {
|
|
|
|
if f == "nps" && !common.IsWindows() && !status(f, pidPath) {
|
|
|
|
log.Println("reload fail")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var c *exec.Cmd
|
|
|
|
var err error
|
|
|
|
b, err := ioutil.ReadFile(filepath.Join(pidPath, f+".pid"))
|
|
|
|
if err == nil {
|
|
|
|
c = exec.Command("/bin/bash", "-c", `kill -30 `+string(b))
|
|
|
|
} else {
|
|
|
|
log.Fatalln("reload error,pid file does not exist")
|
|
|
|
}
|
|
|
|
if c.Run() == nil {
|
|
|
|
log.Println("reload success")
|
|
|
|
} else {
|
|
|
|
log.Println("reload fail")
|
2019-02-02 16:54:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-09 09:07:47 +00:00
|
|
|
func status(f string, pidPath string) bool {
|
2019-02-05 16:35:23 +00:00
|
|
|
var cmd *exec.Cmd
|
2019-02-09 09:07:47 +00:00
|
|
|
b, err := ioutil.ReadFile(filepath.Join(pidPath, f+".pid"))
|
2019-02-05 16:35:23 +00:00
|
|
|
if err == nil {
|
2019-02-09 09:07:47 +00:00
|
|
|
if !common.IsWindows() {
|
2019-02-05 16:35:23 +00:00
|
|
|
cmd = exec.Command("/bin/sh", "-c", "ps -ax | awk '{ print $1 }' | grep "+string(b))
|
|
|
|
} else {
|
2019-02-16 12:43:26 +00:00
|
|
|
cmd = exec.Command("tasklist")
|
2019-02-05 16:35:23 +00:00
|
|
|
}
|
|
|
|
out, _ := cmd.Output()
|
|
|
|
if strings.Index(string(out), string(b)) > -1 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-02-09 09:07:47 +00:00
|
|
|
func start(osArgs []string, f string, pidPath, runPath string) {
|
|
|
|
if status(f, pidPath) {
|
2019-02-05 16:35:23 +00:00
|
|
|
log.Printf(" %s is running", f)
|
|
|
|
return
|
|
|
|
}
|
2019-02-02 16:54:43 +00:00
|
|
|
cmd := exec.Command(osArgs[0], osArgs[1:]...)
|
|
|
|
cmd.Start()
|
|
|
|
if cmd.Process.Pid > 0 {
|
2019-02-09 09:07:47 +00:00
|
|
|
log.Println("start ok , pid:", cmd.Process.Pid, "config path:", runPath)
|
2019-02-02 16:54:43 +00:00
|
|
|
d1 := []byte(strconv.Itoa(cmd.Process.Pid))
|
2019-02-09 09:07:47 +00:00
|
|
|
ioutil.WriteFile(filepath.Join(pidPath, f+".pid"), d1, 0600)
|
2019-02-05 16:35:23 +00:00
|
|
|
} else {
|
|
|
|
log.Println("start error")
|
2019-02-02 16:54:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-09 09:07:47 +00:00
|
|
|
func stop(f string, p string, pidPath string) {
|
|
|
|
if !status(f, pidPath) {
|
2019-02-05 16:35:23 +00:00
|
|
|
log.Printf(" %s is not running", f)
|
|
|
|
return
|
|
|
|
}
|
2019-02-02 16:54:43 +00:00
|
|
|
var c *exec.Cmd
|
|
|
|
var err error
|
2019-02-09 09:07:47 +00:00
|
|
|
if common.IsWindows() {
|
2019-02-02 16:54:43 +00:00
|
|
|
p := strings.Split(p, `\`)
|
|
|
|
c = exec.Command("taskkill", "/F", "/IM", p[len(p)-1])
|
2019-02-05 16:35:23 +00:00
|
|
|
} else {
|
2019-02-09 09:07:47 +00:00
|
|
|
b, err := ioutil.ReadFile(filepath.Join(pidPath, f+".pid"))
|
2019-02-02 16:54:43 +00:00
|
|
|
if err == nil {
|
|
|
|
c = exec.Command("/bin/bash", "-c", `kill -9 `+string(b))
|
|
|
|
} else {
|
2019-02-09 09:07:47 +00:00
|
|
|
log.Fatalln("stop error,pid file does not exist")
|
2019-02-02 16:54:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
err = c.Run()
|
|
|
|
if err != nil {
|
2019-02-05 16:35:23 +00:00
|
|
|
log.Println("stop error,", err)
|
2019-02-02 16:54:43 +00:00
|
|
|
} else {
|
2019-02-05 16:35:23 +00:00
|
|
|
log.Println("stop ok")
|
2019-02-02 16:54:43 +00:00
|
|
|
}
|
|
|
|
}
|