dae/cmd/suspend.go

56 lines
1.1 KiB
Go
Raw Normal View History

2023-03-13 23:34:38 +07:00
/*
* SPDX-License-Identifier: AGPL-3.0-only
* Copyright (c) 2022-2024, daeuniverse Organization <dae@v2raya.org>
2023-03-13 23:34:38 +07:00
*/
package cmd
import (
"fmt"
"os"
"strconv"
"strings"
"syscall"
"github.com/daeuniverse/dae/cmd/internal"
"github.com/spf13/cobra"
2023-03-13 23:34:38 +07:00
)
var (
suspendCmd = &cobra.Command{
2023-03-13 23:46:45 +07:00
Use: "suspend [pid]",
Short: "To suspend dae. This command puts dae into no-load state. Recover it by 'dae reload'.",
2023-03-13 23:34:38 +07:00
Run: func(cmd *cobra.Command, args []string) {
internal.AutoSu()
if len(args) == 0 {
_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))}
}
pid, err := strconv.Atoi(args[0])
if err != nil {
cmd.Help()
os.Exit(1)
}
if abort {
if f, err := os.Create(AbortFile); err == nil {
f.Close()
}
}
if err = syscall.Kill(pid, syscall.SIGUSR2); err != nil {
2023-03-13 23:34:38 +07:00
fmt.Println(err)
os.Exit(1)
}
fmt.Println("OK")
},
}
)
func init() {
rootCmd.AddCommand(suspendCmd)
suspendCmd.PersistentFlags().BoolVarP(&abort, "abort", "a", false, "Abort established connections.")
2023-03-13 23:34:38 +07:00
}