Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alist-org
GitHub Repository: alist-org/alist
Path: blob/main/cmd/start.go
1541 views
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
}
72
73