besy
The process skeleton the lab daemons share, so each of them stops
copy-pasting the same boilerplate: logging, the standard -v/-m/-d CLI
options, daemonizing, build info for --version, and graceful signal
handling -- one dependency, one idiom, every daemon the same shape.
[dependencies]
besy = { git = "https://git.3lp.cx/dyadkin/besy", features = ["clap", "daemon"] }
[build-dependencies]
besy = { git = "https://git.3lp.cx/dyadkin/besy", default-features = false, features = ["build"] }
Features
| feature | default | what it adds |
|---|---|---|
signals |
yes | handle_signals(): SIGHUP ignored, SIGTERM/SIGINT/SIGQUIT stop |
clap |
no | Options: the -v log level and -m dated/no-dated flags |
daemon |
no | Daemon: -d fork, -o/-e log files, -i pid file, -u/-g/-w |
build |
no | build::emit() for build.rs: vergen git/build info + BUILD_YEAR |
debug, trace, info, warn, error and LevelFilter are
re-exported from log, so a binary needs no direct log dependency.
The idiom
A daemon is a sync main that parses, forks, sets up logging and hands
off to an async fn run that owns the tokio runtime -- the runtime
starts AFTER the fork, because worker threads do not survive daemonizing:
// cli.rs
pub const ABOUT: &str = besy::about!(2011);
#[derive(Parser)]
#[command(version, about = ABOUT)]
pub struct Cli {
#[command(flatten)]
pub log: besy::Options,
#[command(flatten)]
pub daemon: besy::Daemon,
#[command(subcommand)]
command: Command,
}
impl Cli {
/// The runtime starts here, after `main` has forked.
#[tokio::main]
pub async fn run(self) -> ExitCode { /* ... */ }
}
// main.rs
fn main() -> ExitCode {
let cli = Cli::parse();
cli.daemon.daemonize();
cli.log.init(cli.daemon.is_daemon());
cli.run()
}
The resulting CLI is identical in every daemon:
-v, --debug... Turn debugging information on (0: errors, 1: warn, 2: info, 3: debug, 4+: trace)
-m, --dated Remove dates from log messages (default is dated; always dated when daemonized)
-d, --daemon Daemonize and run in the background
-o, --out <LOGFILE> Standard log file (requires -d)
-e, --err <LOGFILE> Error log file (requires -d, defaults to -o)
-i, --pid <PIDFILE> PID file, removed on exit (requires -d)
-u, --user <USER> User for daemon (requires -d)
-g, --group <GROUP> Group for daemon (requires -d)
-w, --workdir <WORKDIR> Working directory for daemon (requires -d)
Logging without clap
The bare minimum, no CLI at all:
use besy::{info, Logger, LevelFilter};
fn main() {
Logger::init(true, LevelFilter::Info).expect("could not set logger");
info!("hello from besy");
}
init(dated, level): dated prefixes every line with
YYYY-MM-DD HH:MM:SS.usec: . Options::init(daemon) does the same with
the parsed flags (and forces dates on in daemon mode, where the log goes
to a file).
Build info and --version
build::emit() goes into build.rs. It writes the vergen
VERGEN_BUILD_TIMESTAMP / VERGEN_GIT_SHA / VERGEN_GIT_COMMIT_TIMESTAMP
env vars plus BUILD_YEAR, and the about! macro assembles the standard
block from them and the crate's Cargo.toml (needs description,
authors, homepage, repository):
// build.rs
use std::error::Error;
fn main() -> Result<(), Box<dyn Error>> {
besy::build::emit()
}
// cli.rs
pub const ABOUT: &str = besy::about!(2011);
// (c) line reads: (c) <authors> 2011-<build year>, SNBL@ESRF
Feed the constant to #[command(version, about = ABOUT)] for --version
and the help header, and println! it at startup if the daemon logs to a
file:
example 0.1.0 - a small daemon
(c) Vadim Dyadkin <dyadkin@3lp.cx> 2020-2026, SNBL@ESRF
Homepage: https://git.3lp.cx/dyadkin/example
Repository: https://git.3lp.cx/dyadkin/example
Built: 2026-01-02T03:04:05.000000000Z
Commit: abcdef0123456789abcdef0123456789abcdef01 (2026-01-02T03:04:05+02:00)
Signals
handle_signals() (feature signals, on by default) is an async fn that
returns when the process should stop. SIGHUP is logged and ignored
(config reloading is the daemon's own business); SIGTERM, SIGINT and
SIGQUIT log "gracefully shutting down" and it returns:
use besy::handle_signals;
let shutdown = handle_signals();
tokio::pin!(shutdown);
loop {
tokio::select! {
() = &mut shutdown => break,
accepted = listener.accept() => { /* ... */ }
}
}
Note the startup window: until handle_signals() is called the handlers
are not registered, so a SIGTERM in that window still kills the process
the default way. Register it as early as the serve loop allows.
Windows
daemon degrades to a no-op off-unix (the flags exist so the CLI stays
identical), signals is unix-only, everything else is the same. The
build feature additionally stubs libgcc_eh.a for the
x86_64-pc-windows-gnu target.