-
Notifications
You must be signed in to change notification settings - Fork 25
/
service_test.go
55 lines (44 loc) · 1.2 KB
/
service_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
package service_test
import "gopkg.in/hlandau/service.v3"
// The following example illustrates the minimal skeleton structure to
// implement a daemon. This example can run as a service on Windows or a daemon
// on Linux. The systemd notify protocol is supported.
func Example() {
service.Main(&service.Info{
Title: "Foobar Web Server",
Name: "foobar",
Description: "Foobar Web Server is the greatest webserver ever.",
Config: service.Config{
Daemon: true,
Stderr: true,
PIDFile: "/run/foobar.pid",
UID: "nobody",
Chroot: "/var/empty",
},
RunFunc: func(smgr service.Manager) error {
// Start up your service.
// ...
// Once initialization requiring root is done, call this.
err := smgr.DropPrivileges()
if err != nil {
return err
}
// When it is ready to serve requests, call this.
// You must call DropPrivileges first.
smgr.SetStarted()
// Optionally set a status
smgr.SetStatus("foobar: running ok")
loop:
for {
select {
// Handle requests, or do so in another goroutine controlled from here.
case <-smgr.StopChan():
break loop
}
}
// Do any necessary teardown.
// ...
return nil
},
})
}