@@ -2,8 +2,12 @@ package main
2
2
3
3
import (
4
4
"fmt"
5
+ "io/ioutil"
5
6
"log"
6
7
"os"
8
+ "path/filepath"
9
+ "runtime"
10
+ "strings"
7
11
8
12
_ "github.com/beego/beego/v2/server/web/session/memcache"
9
13
_ "github.com/beego/beego/v2/server/web/session/mysql"
@@ -15,6 +19,21 @@ import (
15
19
_ "github.com/mindoc-org/mindoc/routers"
16
20
)
17
21
22
+ func isViaDaemonUnix () bool {
23
+ parentPid := os .Getppid ()
24
+
25
+ cmdLineBytes , err := ioutil .ReadFile (fmt .Sprintf ("/proc/%d/cmdline" , parentPid ))
26
+ if err != nil {
27
+ return false
28
+ }
29
+
30
+ cmdLine := string (cmdLineBytes )
31
+ executable := strings .Split (cmdLine , " " )[0 ]
32
+ fmt .Printf ("Parent executable: %s\n " , executable )
33
+ filename := filepath .Base (executable )
34
+ return strings .Contains (filename , "mindoc-daemon" )
35
+ }
36
+
18
37
func main () {
19
38
20
39
if len (os .Args ) >= 3 && os .Args [1 ] == "service" {
@@ -30,14 +49,19 @@ func main() {
30
49
31
50
d := daemon .NewDaemon ()
32
51
33
- s , err := service .New (d , d .Config ())
52
+ if runtime .GOOS != "windows" && ! isViaDaemonUnix () {
53
+ s , err := service .New (d , d .Config ())
34
54
35
- if err != nil {
36
- fmt .Println ("Create service error => " , err )
37
- os .Exit (1 )
38
- }
55
+ if err != nil {
56
+ fmt .Println ("Create service error => " , err )
57
+ os .Exit (1 )
58
+ }
39
59
40
- if err := s .Run (); err != nil {
41
- log .Fatal ("启动程序失败 ->" , err )
60
+ if err := s .Run (); err != nil {
61
+ log .Fatal ("启动程序失败 ->" , err )
62
+ }
63
+ } else {
64
+ d .Run ()
42
65
}
66
+
43
67
}
0 commit comments