daemon

package
v0.26.0 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2026 License: MIT Imports: 7 Imported by: 0

README

climate/daemon

Simple utilities for long-running processes and graceful shutdowns.

daemon provides helpers for programs that need to run in the background or stay active until interrupted.

Installation

go get github.com/grimdork/climate/daemon

Graceful shutdown with BreakChannel

Returns a channel that blocks until the program receives SIGINT or SIGTERM. Also cleans up the ^C output from the terminal.

package main

import (
	"fmt"
	"github.com/grimdork/climate/daemon"
)

func main() {
	fmt.Println("Server starting... Press Ctrl+C to stop.")

	// Start listeners, workers, etc.

	<-daemon.BreakChannel()

	fmt.Println("Shutting down gracefully...")
	// Cleanup here
}

Privilege dropping with DegradeToUser

Drop from root to a specified user. Useful for services that bind to privileged ports before switching to a less privileged account.

err := daemon.DegradeToUser("www-data")
if err != nil {
	// Either not running as root (daemon.ErrorNotRoot) or user lookup failed
	log.Fatal(err)
}

Sets the effective UID and primary GID of the specified user. Returns daemon.ErrorNotRoot if the process is not running as root.

Documentation

Overview

Package daemon has a few handy utility functions for server software.

Index

Constants

This section is empty.

Variables

View Source
var ErrorNotRoot = errors.New("not running as root, so can't drop privileges to specified user")

ErrorNotRoot is returned when the user doesn't have root privileges.

Functions

func BreakChannel

func BreakChannel() chan bool

BreakChannel generates a goroutine which waits for ctrl-C to be pressed, and returns a channel to wait on. Using it can be as simple as:

<-daemon.BreakChannel()

func DegradeToUser

func DegradeToUser(uname string) error

DegradeToUser drops down to a specific user and its primary group if run by root.

Types

This section is empty.

Jump to

Keyboard shortcuts

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