mirror of
https://github.com/Mrs4s/go-cqhttp.git
synced 2025-05-06 03:53:50 +08:00
daemon test
This commit is contained in:
parent
5049a8f697
commit
dcd0bfc5e0
8
main.go
8
main.go
@ -7,6 +7,7 @@ import (
|
|||||||
"crypto/sha1"
|
"crypto/sha1"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/Mrs4s/go-cqhttp/global/terminal"
|
"github.com/Mrs4s/go-cqhttp/global/terminal"
|
||||||
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
|
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
|
||||||
@ -119,6 +120,13 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
|
var d bool
|
||||||
|
flag.BoolVar(&d, "d", false, "running as a daemon")
|
||||||
|
flag.Parse()
|
||||||
|
if d {
|
||||||
|
server.Daemon()
|
||||||
|
}
|
||||||
var byteKey []byte
|
var byteKey []byte
|
||||||
arg := os.Args
|
arg := os.Args
|
||||||
if len(arg) > 1 {
|
if len(arg) > 1 {
|
||||||
|
59
server/daemon.go
Normal file
59
server/daemon.go
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
//daemon 功能写在这,目前仅支持了-d 作为后台运行参数,stop,start,restart这些功能目前看起来并不需要,可以通过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
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user