Created
November 3, 2019 02:13
-
-
Save syahrul12345/eb9d83ac2cd722659237f6bdf0356d68 to your computer and use it in GitHub Desktop.
Daemonize a go process
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"net/http" | |
"os" | |
"path/filepath" | |
"github.com/gorilla/mux" | |
"github.com/rs/cors" | |
"github.com/sevlyar/go-daemon" | |
) | |
func main() { | |
cntxt := &daemon.Context{ | |
PidFileName: "sample.pid", | |
PidFilePerm: 0644, | |
LogFileName: "sample.log", | |
LogFilePerm: 0640, | |
WorkDir: "./", | |
Umask: 027, | |
Args: []string{"[go-daemon sample]"}, | |
} | |
d, err := cntxt.Reborn() | |
if err != nil { | |
log.Fatal("Unable to run: ", err) | |
} | |
if d != nil { | |
return | |
} | |
defer cntxt.Release() | |
log.Print("- - - - - - - - - - - - - - -") | |
log.Print("daemon started") | |
serve() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment