|
| 1 | +/* |
| 2 | +Copyright © 2022 NAME HERE <EMAIL ADDRESS> |
| 3 | +*/ |
| 4 | +package cmd |
| 5 | + |
| 6 | +import ( |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + "path/filepath" |
| 10 | + "strconv" |
| 11 | + |
| 12 | + log "github.com/sirupsen/logrus" |
| 13 | + "github.com/spf13/cobra" |
| 14 | +) |
| 15 | + |
| 16 | +// startCmd represents the start command |
| 17 | +var startCmd = &cobra.Command{ |
| 18 | + Use: "start", |
| 19 | + Short: "Silent start alist server with `--force-bin-dir`", |
| 20 | + Run: func(cmd *cobra.Command, args []string) { |
| 21 | + start() |
| 22 | + }, |
| 23 | +} |
| 24 | + |
| 25 | +func start() { |
| 26 | + initDaemon() |
| 27 | + if pid != -1 { |
| 28 | + _, err := os.FindProcess(pid) |
| 29 | + if err == nil { |
| 30 | + log.Info("alist already started, pid ", pid) |
| 31 | + return |
| 32 | + } |
| 33 | + } |
| 34 | + args := os.Args |
| 35 | + args[1] = "server" |
| 36 | + args = append(args, "--force-bin-dir") |
| 37 | + cmd := &exec.Cmd{ |
| 38 | + Path: args[0], |
| 39 | + Args: args, |
| 40 | + Env: os.Environ(), |
| 41 | + } |
| 42 | + stdout, err := os.OpenFile(filepath.Join(filepath.Dir(pidFile), "start.log"), os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666) |
| 43 | + if err != nil { |
| 44 | + log.Fatal(os.Getpid(), ": failed to open start log file:", err) |
| 45 | + } |
| 46 | + cmd.Stderr = stdout |
| 47 | + cmd.Stdout = stdout |
| 48 | + err = cmd.Start() |
| 49 | + if err != nil { |
| 50 | + log.Fatal("failed to start children process: ", err) |
| 51 | + } |
| 52 | + log.Infof("success start pid: %d", cmd.Process.Pid) |
| 53 | + err = os.WriteFile(pidFile, []byte(strconv.Itoa(cmd.Process.Pid)), 0666) |
| 54 | + if err != nil { |
| 55 | + log.Warn("failed to record pid, you may not be able to stop the program with `./alist stop`") |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func init() { |
| 60 | + rootCmd.AddCommand(startCmd) |
| 61 | + |
| 62 | + // Here you will define your flags and configuration settings. |
| 63 | + |
| 64 | + // Cobra supports Persistent Flags which will work for this command |
| 65 | + // and all subcommands, e.g.: |
| 66 | + // startCmd.PersistentFlags().String("foo", "", "A help for foo") |
| 67 | + |
| 68 | + // Cobra supports local flags which will only run when this command |
| 69 | + // is called directly, e.g.: |
| 70 | + // startCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") |
| 71 | +} |
0 commit comments