sevlyar / go-daemon

A library for writing system daemons in golang.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

generates a blank pid file on Mac (Darwin)

Shashwatsh opened this issue · comments

package main

import (
	"fmt"
	"github.com/sevlyar/go-daemon"
	"html"
	"log"
	"net/http"
	"os"
)

// To terminate the daemon use:
//  kill `cat pid`
func main()  {

	fmt.Println(os.Args[1])
	if os.Args[1] == "test" {
		test("hello,world")
	}
	if os.Args[1] == "hello" {
		serveHttp()
	}

}

func test(t string) {
	cntxt := &daemon.Context{
		PidFileName: "pid",
		PidFilePerm: 0644,
		LogFileName: "log",
		LogFilePerm: 0640,
		WorkDir:     "./test2/",
		Umask:       027,
		Args:        []string{"[go-daemon sample]", "hello"},
	}

	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")

	log.Print(t)
}

func serveHttp() {

	log.Print("testing!")
	http.HandleFunc("/", httpHandler)
	http.ListenAndServe("127.0.0.1:3080", nil)
}

func httpHandler(w http.ResponseWriter, r *http.Request) {
	log.Printf("request from %s: %s %q", r.RemoteAddr, r.Method, r.URL)
	fmt.Fprintf(w, "go-daemon: %q", html.EscapeString(r.URL.Path))
}

it just saves a blank PID file...Nope..Nothing on it...No PIDs

@Shashwatsh please provide an output of uname -a && go version && go env command.

Darwin Shashwats-MacBook-Air.local 16.6.0 Darwin Kernel Version 16.6.0: Fri Apr 14 16:21:16 PDT 2017; root:xnu-3789.60.24~6/RELEASE_X86_64 x86_64
go version go1.8.3 darwin/amd64
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/shashwat/go"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.8.3/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.8.3/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/kg/fqkyc3h968s3dqd079_zhblm0000gn/T/go-build029181875=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"

Thank you, @Shashwatsh !
Duplicates #22