{"id":13756329,"url":"https://github.com/takama/daemon","last_synced_at":"2025-05-14T19:09:09.908Z","repository":{"id":19301537,"uuid":"22539035","full_name":"takama/daemon","owner":"takama","description":"A daemon package for use with Go (golang) services","archived":false,"fork":false,"pushed_at":"2023-12-01T07:26:13.000Z","size":212,"stargazers_count":1972,"open_issues_count":28,"forks_count":291,"subscribers_count":60,"default_branch":"master","last_synced_at":"2025-04-13T13:28:45.932Z","etag":null,"topics":["daemon","go","golang","linux","service"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/takama.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-08-02T04:10:34.000Z","updated_at":"2025-04-11T08:32:41.000Z","dependencies_parsed_at":"2024-06-18T11:30:00.936Z","dependency_job_id":null,"html_url":"https://github.com/takama/daemon","commit_stats":{"total_commits":170,"total_committers":23,"mean_commits":7.391304347826087,"dds":0.3294117647058824,"last_synced_commit":"496d69192531c6cb1004380a0be0fcf2031b06f4"},"previous_names":[],"tags_count":52,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/takama%2Fdaemon","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/takama%2Fdaemon/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/takama%2Fdaemon/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/takama%2Fdaemon/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/takama","download_url":"https://codeload.github.com/takama/daemon/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254209859,"owners_count":22032897,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["daemon","go","golang","linux","service"],"created_at":"2024-08-03T11:00:42.027Z","updated_at":"2025-05-14T19:09:07.899Z","avatar_url":"https://github.com/takama.png","language":"Go","funding_links":[],"categories":["Go"],"sub_categories":[],"readme":"# Go Daemon\n\nA daemon package for use with Go (golang) services\n\n[![GoDoc](https://godoc.org/github.com/takama/daemon?status.svg)](https://godoc.org/github.com/takama/daemon)\n\n## Examples\n\n### Simplest example (just install self as daemon)\n\n```go\npackage main\n\nimport (\n    \"fmt\"\n    \"log\"\n\n    \"github.com/takama/daemon\"\n)\n\nfunc main() {\n    service, err := daemon.New(\"name\", \"description\", daemon.SystemDaemon)\n    if err != nil {\n        log.Fatal(\"Error: \", err)\n    }\n    status, err := service.Install()\n    if err != nil {\n        log.Fatal(status, \"\\nError: \", err)\n    }\n    fmt.Println(status)\n}\n```\n\n### Real example\n\n```go\n// Example of a daemon with echo service\npackage main\n\nimport (\n    \"fmt\"\n    \"log\"\n    \"net\"\n    \"os\"\n    \"os/signal\"\n    \"syscall\"\n\n    \"github.com/takama/daemon\"\n)\n\nconst (\n\n    // name of the service\n    name        = \"myservice\"\n    description = \"My Echo Service\"\n\n    // port which daemon should be listen\n    port = \":9977\"\n)\n\n//    dependencies that are NOT required by the service, but might be used\nvar dependencies = []string{\"dummy.service\"}\n\nvar stdlog, errlog *log.Logger\n\n// Service has embedded daemon\ntype Service struct {\n    daemon.Daemon\n}\n\n// Manage by daemon commands or run the daemon\nfunc (service *Service) Manage() (string, error) {\n\n    usage := \"Usage: myservice install | remove | start | stop | status\"\n\n    // if received any kind of command, do it\n    if len(os.Args) \u003e 1 {\n        command := os.Args[1]\n        switch command {\n        case \"install\":\n            return service.Install()\n        case \"remove\":\n            return service.Remove()\n        case \"start\":\n            return service.Start()\n        case \"stop\":\n            return service.Stop()\n        case \"status\":\n            return service.Status()\n        default:\n            return usage, nil\n        }\n    }\n\n    // Do something, call your goroutines, etc\n\n    // Set up channel on which to send signal notifications.\n    // We must use a buffered channel or risk missing the signal\n    // if we're not ready to receive when the signal is sent.\n    interrupt := make(chan os.Signal, 1)\n    signal.Notify(interrupt, os.Interrupt, os.Kill, syscall.SIGTERM)\n\n    // Set up listener for defined host and port\n    listener, err := net.Listen(\"tcp\", port)\n    if err != nil {\n        return \"Possibly was a problem with the port binding\", err\n    }\n\n    // set up channel on which to send accepted connections\n    listen := make(chan net.Conn, 100)\n    go acceptConnection(listener, listen)\n\n    // loop work cycle with accept connections or interrupt\n    // by system signal\n    for {\n        select {\n        case conn := \u003c-listen:\n            go handleClient(conn)\n        case killSignal := \u003c-interrupt:\n            stdlog.Println(\"Got signal:\", killSignal)\n            stdlog.Println(\"Stoping listening on \", listener.Addr())\n            listener.Close()\n            if killSignal == os.Interrupt {\n                return \"Daemon was interruped by system signal\", nil\n            }\n            return \"Daemon was killed\", nil\n        }\n    }\n\n    // never happen, but need to complete code\n    return usage, nil\n}\n\n// Accept a client connection and collect it in a channel\nfunc acceptConnection(listener net.Listener, listen chan\u003c- net.Conn) {\n    for {\n        conn, err := listener.Accept()\n        if err != nil {\n            continue\n        }\n        listen \u003c- conn\n    }\n}\n\nfunc handleClient(client net.Conn) {\n    for {\n        buf := make([]byte, 4096)\n        numbytes, err := client.Read(buf)\n        if numbytes == 0 || err != nil {\n            return\n        }\n        client.Write(buf[:numbytes])\n    }\n}\n\nfunc init() {\n    stdlog = log.New(os.Stdout, \"\", log.Ldate|log.Ltime)\n    errlog = log.New(os.Stderr, \"\", log.Ldate|log.Ltime)\n}\n\nfunc main() {\n    srv, err := daemon.New(name, description, daemon.SystemDaemon, dependencies...)\n    if err != nil {\n        errlog.Println(\"Error: \", err)\n        os.Exit(1)\n    }\n    service := \u0026Service{srv}\n    status, err := service.Manage()\n    if err != nil {\n        errlog.Println(status, \"\\nError: \", err)\n        os.Exit(1)\n    }\n    fmt.Println(status)\n}\n```\n\n### Service config file\n\nOptionally, service config file can be retrieved or updated by calling\n`GetTemplate() string` and `SetTemplate(string)` methods(except MS\nWindows). Template will be a default Go Template(`\"text/template\"`).\n\nIf `SetTemplate` is not called, default template content will be used\nwhile creating service.\n\n| Variable     | Description                      |\n| ------------ | -------------------------------- |\n| Description  | Description for service          |\n| Dependencies | Service dependencies             |\n| Name         | Service name                     |\n| Path         | Path of service executable       |\n| Args         | Arguments for service executable |\n\n#### Example template(for linux systemv)\n\n```ini\n[Unit]\nDescription={{.Description}}\nRequires={{.Dependencies}}\nAfter={{.Dependencies}}\n\n[Service]\nPIDFile=/var/run/{{.Name}}.pid\nExecStartPre=/bin/rm -f /var/run/{{.Name}}.pid\nExecStart={{.Path}} {{.Args}}\nRestart=on-failure\n\n[Install]\nWantedBy=multi-user.target\n```\n\n### Cron example\n\nSee `examples/cron/cron_job.go`\n\n## Contributors (unsorted)\n\n- [Sheile](https://github.com/Sheile)\n- [Nguyen Trung Loi](https://github.com/loint)\n- [Donny Prasetyobudi](https://github.com/donnpebe)\n- [Mark Berner](https://github.com/mark2b)\n- [Fatih Kaya](https://github.com/fatihky)\n- [Jannick Fahlbusch](https://github.com/jannickfahlbusch)\n- [TobyZXJ](https://github.com/tobyzxj)\n- [Pichu Chen](https://github.com/PichuChen)\n- [Eric Halpern](https://github.com/ehalpern)\n- [Yota](https://github.com/nus)\n- [Erkan Durmus](https://github.com/derkan)\n- [maxxant](https://github.com/maxxant)\n- [1for](https://github.com/1for)\n- [okamura](https://github.com/sidepelican)\n- [0X8C - Demired](https://github.com/Demired)\n- [Maximus](https://github.com/maximus12793)\n- [AlgorathDev](https://github.com/AlgorathDev)\n- [Alexis Camilleri](https://github.com/krysennn)\n- [neverland4u](https://github.com/neverland4u)\n- [Rustam](https://github.com/rusq)\n- [King'ori Maina](https://github.com/itskingori)\n\nAll the contributors are welcome. If you would like to be the contributor please accept some rules.\n\n- The pull requests will be accepted only in `develop` branch\n- All modifications or additions should be tested\n- Sorry, We will not accept code with any dependency, only standard library\n\nThank you for your understanding!\n\n## License\n\n[MIT Public License](https://github.com/takama/daemon/blob/master/LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftakama%2Fdaemon","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftakama%2Fdaemon","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftakama%2Fdaemon/lists"}