gdaemon

package
v0.0.0-...-37ff87a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 26, 2024 License: MIT Imports: 11 Imported by: 0

README

Daemon

daemon package does add daemon, logging and forever function to your program.

the below command line arguments you can passed to your program after you using daemon package.

  1. --forver or -forver

the argument will fork a worker process and master process monit worker process , and restart it when it crashed.

  1. --daemon or -daemon

the argument will put your program running in background , only working in linux uinx etc.

  1. --flog or -flog <filename.log>

the argument will logging your program stdout to the log file.

  1. notice

you maybe execute your program without "daemon package" like this:

./foobar -u root

after using daemon package, execute your program can be like this:

./foobar -u root -forever -daemon -flog foobar.log

Demo

package main

import (
	"fmt"
	"os"
	"os/signal"
	"syscall"

	"github.com/snail007/gmc/process/daemon"
)

func main() {
	//the code block should be in your main function,and the actual main() change to doMain()
	if err := gdaemon.Start(); err != nil {
		fmt.Println(err)
		return
	}
	if gdaemon.CanRun() {
		//call actual main()
		go doMain()
	}
	//waiting kill signal
	signalChan := make(chan os.Signal, 1)
	signal.Notify(signalChan, os.Interrupt, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
	<-signalChan
	//do clean
	gdaemon.Clean()
}

//your main function
func doMain() {
	//flag.Parse() should be called here if you have
}

Documentation

Overview

Package gdaemon does add daemon, logging and forever function to your program.

the below command line arguments you can passed to your program after you using daemon package.

--forver or -forver

the argument will fork a worker process and master process monit worker process , and restart it when it crashed.

--daemon or -daemon

the argument will put your program running in background , only working in linux uinx etc.

--flog or -flog <filename.log>

the argument will logging your program stdout to the log file.

notice:

before you maybe execute your program like this:

./foobar -u root

after using daemon package, execute your program can be like this:

./foobar -u root -forever -daemon -flog foobar.log

Example
package main

import (
	"fmt"
	"os"
	"os/signal"
	"syscall"
)

func main() {
	//the code block should be in your main function,and the actual main() change to doMain()
	if err := Start(); err != nil {
		fmt.Println(err)
		return
	}
	if CanRun() {
		//call actual main()
		go doMain()
	}
	//waiting kill signal
	signalChan := make(chan os.Signal, 1)
	signal.Notify(signalChan, os.Interrupt, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
	<-signalChan
	//do clean
	Clean()
}

// your main function
func doMain() {
	//flag.Parse() should be called here if you have
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CanRun

func CanRun() bool

CanRun check if program can be run

func Clean

func Clean()

Clean process, should be call before program exit.

func InitFlags

func InitFlags()

func SetLogger

func SetLogger(l *logger.Logger)

SetLogger sets the logger for logging

default is logger.New(os.Stderr, "", logger.LstdFlags)

you can SetLogger(nil) to disable logging

func Start

func Start() (err error)

Start daemon or forever or flog

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL