takama / daemon

A daemon package for use with Go (golang) services

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Flags in parametres don't work!

capsulated opened this issue · comments

Hello! I read two topics about keys in parameters, when intsall service (like 'sudo ./myservice install -myflag "flagValue"'), but it didn't work!

Example here:
// Example of a daemon with echo service
package main

import (
"fmt"
"log"
"os"
"os/signal"
"syscall"
"github.com/takama/daemon"
"flag"
)

const (
// name of the service
name = "myservice"
description = "My Echo Service"
// port which daemon should be listen
port = ":9977"
)

//dependencies that are NOT required by the service, but might be used
var dependencies = []string{"dummy.service"}

var stdlog, errlog *log.Logger

// Service has embedded daemon
type Service struct {
daemon.Daemon
}

// Manage by daemon commands or run the daemon
func (service *Service) Manage() (string, error) {
usage := "Usage: myservice install | remove | start | stop | status"
// if received any kind of command, do it
if len(os.Args) > 1 {
command := os.Args[1]
switch command {
case "install":
return service.Install()
case "remove":
return service.Remove()
case "start":
return service.Start()
case "stop":
return service.Stop()
case "status":
return service.Status()
//default:
// return usage, nil
}
}
// Do something, call your goroutines, etc
var customFlag string
flag.StringVar(&customFlag, "c", customFlag, "custom flag")
flag.Parse()
stdlog.Println("custom flag is: ", customFlag)
// Set up channel on which to send signal notifications.
// We must use a buffered channel or risk missing the signal
// if we're not ready to receive when the signal is sent.
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, os.Kill, syscall.SIGTERM)
// loop work cycle with accept connections or interrupt
// by system signal
for {
select {
case killSignal := <-interrupt:
stdlog.Println("Got signal:", killSignal)
if killSignal == os.Interrupt {
return "Daemon was interruped by system signal", nil
}
return "Daemon was killed", nil
}
}
// never happen, but need to complete code
return usage, nil
}
func init() {
f, err := os.OpenFile("/Users/myname/go/daemon/test.log", os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
fmt.Printf("error opening file: %v", err)
}
stdlog = log.New(f, "", log.Ldate|log.Ltime)
errlog = log.New(f, "", log.Ldate|log.Ltime)
}
func main() {
srv, err := daemon.New(name, description, dependencies...)
if err != nil {
errlog.Println("Error: ", err)
os.Exit(1)
}
service := &Service{srv}
status, err := service.Manage()
if err != nil {
errlog.Println(status, "\nError: ", err)
os.Exit(1)
}
fmt.Println(status)
}

Maybe i do something wrong?
Thanks.

Hi,

Your code for flags parsing won't work with install command together due to return statement above

	case "install":
		return service.Install()
var customFlag string
flag.StringVar(&customFlag, "c", customFlag, "custom flag")
flag.Parse()

All flags, which used with install command should register as cmd args in corresponded init script

[Service]
...
ExecStart={{.Path}} {{.Args}}
Restart=on-abort