-
Notifications
You must be signed in to change notification settings - Fork 0
/
daemon.go
61 lines (51 loc) · 1.21 KB
/
daemon.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"flag"
"github.com/sevlyar/go-daemon"
"log"
"log/syslog"
"os"
"path/filepath"
)
var (
flagDaemon = flag.Bool("daemon", false, "daemonize process")
flagPidFile = flag.String("daemon.pidfile", "", "pid file name")
// flagLogFile = flag.String("daemon.logfile", "", "logfile file name")
// flagLogFilePerm = flag.Int("daemon.logfileperm", 0640, "logfile permissions")
flagChroot = flag.String("daemon.chroot", "", "chroot directory")
flagSyslog = flag.Bool("daemon.syslog", true, "log to syslog")
)
func daemonize(f func()) {
if !*flagDaemon {
f()
return
}
cntx := &daemon.Context{
PidFileName: *flagPidFile,
PidFilePerm: 0,
//LogFileName: *flagLogFile,
//LogFilePerm: os.FileMode(*flagLogFilePerm),
Chroot: *flagChroot,
}
d, err := cntx.Reborn()
if err != nil {
logFatal("unable to daemonize: %s", err)
}
if d != nil {
return
}
defer func() {
err := cntx.Release()
if err != nil {
logFatal("cannot release daemon context: %s", err)
}
}()
if *flagSyslog {
sysl, err := syslog.New(syslog.LOG_INFO|syslog.LOG_USER, filepath.Base(os.Args[0]))
if err != nil {
logFatal("cannot open syslog: %s", err)
}
logger = log.New(sysl, "", log.Flags())
}
f()
}