Skip to content

Instantly share code, notes, and snippets.

@syahrul12345
Created November 3, 2019 02:13
Show Gist options
  • Save syahrul12345/eb9d83ac2cd722659237f6bdf0356d68 to your computer and use it in GitHub Desktop.
Save syahrul12345/eb9d83ac2cd722659237f6bdf0356d68 to your computer and use it in GitHub Desktop.
Daemonize a go process
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