1
0
mirror of https://github.com/Mrs4s/go-cqhttp.git synced 2025-05-04 19:17:37 +08:00
go-cqhttp/server/daemon.go
2021-03-10 16:12:10 +08:00

60 lines
1.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//daemon 功能写在这,目前仅支持了-d 作为后台运行参数stopstartrestart这些功能目前看起来并不需要可以通过api控制后续需要的话再补全。
package server
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)
func Daemon() {
args := os.Args[1:]
execArgs := make([]string, 0)
l := len(args)
for i := 0; i < l; i++ {
if strings.Index(args[i], "-d") == 0 {
continue
}
execArgs = append(execArgs, args[i])
}
proc := exec.Command(os.Args[0], execArgs...)
err := proc.Start()
if err != nil {
panic(err)
}
fmt.Println("[PID] ", proc.Process.Pid)
os.Exit(0)
}
func GetCurrentPath() (string, error) {
file, err := exec.LookPath(os.Args[0])
if err != nil {
return "", err
}
path, err := filepath.Abs(file)
if err != nil {
return "", err
}
//fmt.Println("path111:", path)
if runtime.GOOS == "windows" {
path = strings.Replace(path, "\\", "/", -1)
}
//fmt.Println("path222:", path)
i := strings.LastIndex(path, "/")
if i < 0 {
//return "", errors.New("system/path_error", `Can't find "/" or "\".`)
}
//fmt.Println("path333:", path)
return string(path[0 : i+1]), nil
}