DaemonForge
DaemonForge is a cross-platform library for creating system daemons (background services) in Rust. It abstracts away the low-level complexities of operating system process management, providing a safe, idiomatic, and ergonomic builder API.
This crate is suitable for learning and experimentation, but not recommended for serious or production projects.
Key Features
- True Cross-Platform Support:
- Unix/Linux (Hybrid Mode):
- Systemd Native: Automatically detects if running under Systemd (
NOTIFY_SOCKET). If detected, it stays in the foreground and sendsREADY=1notifications viasd-notify. - Legacy Daemonization: If run manually, it falls back to the canonical
double-forkroutine (setsid,umask, standard IO redirection) to run in the background.
- Systemd Native: Automatically detects if running under Systemd (
- Windows: Uses native "Detached Processes" and manages creation flags for true background execution without a console window (NOT a Windows Service).
- Unix/Linux (Hybrid Mode):
- Locking Mechanism:
- Automatically prevents multiple instances of the same service from running simultaneously.
- Utilizes
flock(Unix) and Global Named Mutexes (Windows) for reliable exclusion.
- Security First:
- Secure environment variable clearing.
- Support for privilege dropping (User/Group switching) and
chrootjail on Unix systems.
- Other features:
- Panic Capture: Redirects
stdout/stderrto log files, ensuring that panics and crashes are recorded instead of being lost.
- Panic Capture: Redirects
Usage Examples
Linux/Unix Example (Systemd & Manual Compatible)
The library now uses an "Inversion of Control" pattern. You pass your main loop closure to privileged_action.
Note: For proper Systemd support, your loop must handle termination signals (like
SIGTERM) to exit cleanly.
use ForgeDaemon;
use OpenOptions;
use Arc;
use ;
use thread;
use Duration;
use *;
use flag;
Windows Example
On Windows, it is highly recommended to set a .name() for your daemon. This creates a global mutex to ensure uniqueness.
use ForgeDaemon;
use OpenOptions;
use env;
Systemd Integration (Linux)
To use the Systemd features, create a service file at /etc/systemd/system/my_service.service.
Crucial Settings:
Type=notify: Tells Systemd to wait for theREADY=1signal sent by DaemonForge.ExecStart: Must be the absolute path to your binary.
[Unit]
Description=My Rust Daemon
[Service]
# Important: This allows DaemonForge to handshake with Systemd
Type=notify
# Replace with your actual path
ExecStart=/path/to/your/release/binary
# Logs will appear in `journalctl -u my_service`
StandardOutput=journal
StandardError=journal
Restart=on-failure
[Install]
WantedBy=multi-user.target
Advanced Configuration
You can execute a privileged_action before the process fully daemonizes. This is useful for tasks that require higher permissions (like binding to port 80) or for checking environment prerequisites.
# use ForgeDaemon;
new
.clear_env // Clears inherited environment variables
.env // Sets explicit variables
.privileged_action;