dae/cmd/reload.go

50 lines
938 B
Go
Raw Normal View History

2023-02-27 13:07:36 +07:00
/*
* SPDX-License-Identifier: AGPL-3.0-only
2023-03-14 14:01:55 +07:00
* Copyright (c) 2023, daeuniverse Organization <dae@v2raya.org>
2023-02-27 13:07:36 +07:00
*/
package cmd
import (
2023-03-13 23:34:38 +07:00
"fmt"
2023-02-27 13:07:36 +07:00
"os"
"strconv"
2023-03-13 23:34:38 +07:00
"strings"
2023-02-27 13:07:36 +07:00
"syscall"
"github.com/daeuniverse/dae/cmd/internal"
"github.com/spf13/cobra"
2023-02-27 13:07:36 +07:00
)
var (
reloadCmd = &cobra.Command{
2023-03-13 23:46:45 +07:00
Use: "reload [pid]",
Short: "To reload config file without interrupt connections.",
2023-02-27 13:07:36 +07:00
Run: func(cmd *cobra.Command, args []string) {
2023-03-13 23:34:38 +07:00
internal.AutoSu()
2023-02-27 13:07:36 +07:00
if len(args) == 0 {
2023-03-13 23:34:38 +07:00
_pid, err := os.ReadFile(PidFilePath)
if err != nil {
fmt.Println("Failed to read pid file:", err)
os.Exit(1)
}
args = []string{strings.TrimSpace(string(_pid))}
2023-02-27 13:07:36 +07:00
}
pid, err := strconv.Atoi(args[0])
if err != nil {
cmd.Help()
os.Exit(1)
}
2023-03-13 23:34:38 +07:00
if err = syscall.Kill(pid, syscall.SIGUSR1); err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("OK")
2023-02-27 13:07:36 +07:00
},
}
)
2023-03-13 23:34:38 +07:00
func init() {
rootCmd.AddCommand(reloadCmd)
2023-03-13 23:46:45 +07:00
}