forked from sevlyar/go-daemon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
daemon_test.go
68 lines (59 loc) · 1.2 KB
/
daemon_test.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
62
63
64
65
66
67
68
package daemon
import (
"flag"
"log"
"os"
"syscall"
"time"
)
func Example() {
signal := flag.String("s", "", "send signal to daemon")
handler := func(sig os.Signal) error {
log.Println("signal:", sig)
if sig == syscall.SIGTERM {
return ErrStop
}
return nil
}
// Define command: command-line arg, system signal and handler
AddCommand(StringFlag(signal, "term"), syscall.SIGTERM, handler)
AddCommand(StringFlag(signal, "reload"), syscall.SIGHUP, handler)
flag.Parse()
// Define daemon context
dmn := &Context{
PidFileName: "/var/run/daemon.pid",
PidFilePerm: 0644,
LogFileName: "/var/log/daemon.log",
LogFilePerm: 0640,
WorkDir: "/",
Umask: 027,
}
// Send commands if needed
if len(ActiveFlags()) > 0 {
d, err := dmn.Search()
if err != nil {
log.Fatalln("Unable send signal to the daemon:", err)
}
SendCommands(d)
return
}
// Process daemon operations - send signal if present flag or daemonize
child, err := dmn.Reborn()
if err != nil {
log.Fatalln(err)
}
if child != nil {
return
}
defer dmn.Release()
// Run main operation
go func() {
for {
time.Sleep(0)
}
}()
err = ServeSignals()
if err != nil {
log.Println("Error:", err)
}
}