进程管理程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pmt/db/worker.go

75 lines
1.5 KiB

package db
import (
"flag"
"fmt"
"log"
"os"
"os/exec"
"time"
)
// 移除不需要的参数
func strip(slice []string, remove string) []string {
for i := 0; i < len(slice); {
if slice[i] == remove {
if i != len(slice)-1 {
slice = append(slice[:i], slice[i+1:]...)
} else {
slice = slice[:i]
}
} else {
i++
}
}
return slice
}
func subProcess(args []string) *exec.Cmd {
cmd := exec.Command(args[0], args[1:]...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Start()
if err != nil {
fmt.Fprintf(os.Stderr, "[-] Error: %s\n", err)
}
return cmd
}
func main() {
daemon := flag.Bool("daemon", false, "run in daemon")
forever := flag.Bool("forever", false, "run forever")
flag.Parse()
// 启用守护模式
if *daemon {
subProcess(strip(os.Args, "-daemon"))
fmt.Printf("[*] Daemon running in pid: %d PPID: %d\n", os.Getpid(), os.Getppid())
os.Exit(0)
}
if *forever {
for {
cmd := subProcess(strip(os.Args, "-forever"))
fmt.Printf("[*] Forever running in pid: %d PPID: %d\n", os.Getpid(), os.Getppid())
if err := cmd.Wait(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
}
fmt.Printf("[*] Service running in pid: %d PPID: %d\n", os.Getpid(), os.Getppid())
fp, _ := os.OpenFile("./dosomething.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
log.SetOutput(fp)
for {
log.Printf("DoSomething running in pid: %d PPID: %d\n", os.Getpid(), os.Getppid())
time.Sleep(time.Second * 5)
}
}
func run() {
}