daemon

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2022 License: GPL-3.0 Imports: 8 Imported by: 4

README

Golang package to daemonize server application

Create new daemon

func main(){
    if len(os.Args) < 2 {
        return
    }

    err := daemon.Init(os.Args[0], map[string]interface{}{}, "./app.pid")
    if err != nil {
        return
    }

    var cmd string
    if len(os.Args) > 1 {
        cmd = os.Args[1]
    }
    
    switch cmd {
    case "start":
        err = daemon.Start()
    case "stop":
        err = daemon.Stop()
    case "restart":
        err = daemon.Stop()
        err = daemon.Start()
    case "status":
        status := "stopped"
        if daemon.IsRun() {
            status = "started"
        }
    
        fmt.Printf("Application is %s\n", status)
    
        return
    case "":
		fallthrough
    default:
        mainLoop()
    }
}

func mainLoop(){
    for {
        log.Println("I'm daemon")
        time.Sleep(time.Minute)
    }
}

To start the script as daemon to need:

$ go build -o daemon.app

$ ./daemon.app start

To stop:

$ ./daemon.app stop

To restart:

$ ./daemon.app restart

To show status:

$ ./daemon.app status

You may see the code in this example.

Create new daemon with full code control

In the case to start to need just compile code and run as usual: $ go build -o daemon.app $ ./daemon.app

NB The script starts as daemon from beginning and will be stopped after 1 minute.

List all methods

  • daemon.Init - initializes daemon
  • daemon.Start - to daemonize script
  • daemon.Stop - to stop of daemonization
  • daemon.IsRun - returns flag of the running
  • daemon.RemovePIDFile() - removes autocreated PID file

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreatePIDFile

func CreatePIDFile() (err error)

func Init

func Init(name string, params map[string]interface{}, pidPath string) (err error)

func IsRun

func IsRun() (ok bool)

func RemovePIDFile

func RemovePIDFile() (err error)

func Start

func Start() (err error)

func Stop

func Stop() (err error)

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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