MCPcopy Index your code
hub / github.com/sevlyar/go-daemon

github.com/sevlyar/go-daemon @v0.1.6

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.1.6 ↗ · + Follow
80 symbols 188 edges 21 files 30 documented · 38% 9 cross-repo links updated 8mo agov0.1.6 · 2022-07-08★ 2,30723 open issues

Browse by type

Functions 72 Types & classes 8
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

go-daemon Build Status GoDoc

Library for writing system daemons in Go.

Now supported only UNIX-based OS (Windows is not supported). But the library was tested only on Linux and OSX, so that if you have an ability to test the library on other platforms, give me feedback, please (#26).

Please, feel free to send me bug reports and fixes. Many thanks to all contributors.

Features

  • Goroutine-safe daemonization;
  • Out of box work with pid-files;
  • Easy handling of system signals;
  • The control of a daemon.

Installation

go get github.com/sevlyar/go-daemon

You can use gopkg.in:

go get gopkg.in/sevlyar/go-daemon.v0

If you want to use the library in production project, please use vendoring, because i can not ensure backward compatibility before release v1.0.

Examples

Documentation

godoc.org/github.com/sevlyar/go-daemon

How it works

We can not use fork syscall in Golang's runtime, because child process doesn't inherit threads and goroutines in that case. The library uses a simple trick: it runs its own copy with a mark - a predefined environment variable. Availability of the variable for the process means an execution in the child's copy. So that if the mark is not setted - the library executes parent's operations and runs its own copy with mark, and if the mark is setted - the library executes child's operations:

import "log"

func main() {
    Pre()

    context := new(Context)
    child, _ := context.Reborn()

    if child != nil {
        PostParent()
    } else {
        defer func() {
            if err := context.Release(); err != nil {
                log.Printf("Unable to release pid-file: %s", err.Error())
            }
        }()

        PostChild()
    }
}

Extension points exported contracts — how you extend this code

Core symbols most depended-on inside this repo

Shape

Function 47
Method 25
Struct 6
FuncType 1
Interface 1

Languages

Go100%

Modules by API surface

command.go14 symbols
lock_file.go10 symbols
daemon_unix.go10 symbols
signal.go5 symbols
lock_file_test.go4 symbols
examples/cmd/gd-signal-handling/signal-handling.go4 symbols
examples/cmd/gd-log-rotation/main.go4 symbols
examples/cmd/gd-log-rotation/log_file.go4 symbols
daemon_stub.go4 symbols
daemon.go4 symbols
examples/cmd/gd-simple/simple.go3 symbols
lock_file_unix.go2 symbols

For agents

$ claude mcp add go-daemon \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page