https://github.com/leprosus/golang-daemon
Golang package to daemonize
https://github.com/leprosus/golang-daemon
daemon daemonize golang
Last synced: 4 months ago
JSON representation
Golang package to daemonize
- Host: GitHub
- URL: https://github.com/leprosus/golang-daemon
- Owner: leprosus
- License: gpl-3.0
- Created: 2017-05-08T08:11:38.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2022-01-02T16:08:35.000Z (over 4 years ago)
- Last Synced: 2024-06-19T23:12:09.875Z (about 2 years ago)
- Topics: daemon, daemonize, golang
- Language: Go
- Size: 22.5 KB
- Stars: 6
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
- License: License.md
Awesome Lists containing this project
README
# Golang package to daemonize server application
## Create new daemon
```go
func main(){
if len(os.Args) < 2 {
return
}
err := daemon.Init(os.Args[0], map[string]interface{}{}, "./app.pid")
if err != nil {
return
}
var cmd string
if len(os.Args) > 1 {
cmd = os.Args[1]
}
switch cmd {
case "start":
err = daemon.Start()
case "stop":
err = daemon.Stop()
case "restart":
err = daemon.Stop()
err = daemon.Start()
case "status":
status := "stopped"
if daemon.IsRun() {
status = "started"
}
fmt.Printf("Application is %s\n", status)
return
case "":
fallthrough
default:
mainLoop()
}
}
func mainLoop(){
for {
log.Println("I'm daemon")
time.Sleep(time.Minute)
}
}
```
To start the script as daemon to need:
`$ go build -o daemon.app`
`$ ./daemon.app start`
To stop:
`$ ./daemon.app stop`
To restart:
`$ ./daemon.app restart`
To show status:
`$ ./daemon.app status`
You may see the code in this [example](./example/deamon.go).
## Create new daemon with full code control
In the case to start to need just compile code and run as usual:
`$ go build -o daemon.app`
`$ ./daemon.app`
NB The script starts as daemon from beginning and will be stopped after 1 minute.
## List all methods
* daemon.Init - initializes daemon
* daemon.Start - to daemonize script
* daemon.Stop - to stop of daemonization
* daemon.IsRun - returns flag of the running
* daemon.RemovePIDFile() - removes autocreated PID file