feat: add daemonization with the daemonize crate

Co-authored-by: aider (gemini/gemini-2.5-pro-preview-05-06) <aider@aider.chat>
This commit is contained in:
Chaos Rogers 2025-07-21 22:05:11 +01:00
parent e243d87018
commit 7bf45c43c9

View file

@ -13,6 +13,7 @@ use crate::serial_input::start_serial_thread;
use crate::sync_logic::LtcState;
use crate::ui::start_ui;
use clap::Parser;
use daemonize::Daemonize;
use env_logger;
use std::{
@ -64,6 +65,28 @@ fn ensure_config() {
async fn main() {
let args = Args::parse();
if let Some(Command::Daemon) = &args.command {
println!("🚀 Starting daemon...");
// Create files for stdout and stderr in the current directory
let stdout = fs::File::create("daemon.out").expect("Could not create daemon.out");
let stderr = fs::File::create("daemon.err").expect("Could not create daemon.err");
let daemonize = Daemonize::new()
.pid_file("ntp_timeturner.pid") // Create a PID file
.working_directory(".") // Keep the same working directory
.stdout(stdout)
.stderr(stderr);
match daemonize.start() {
Ok(_) => { /* Process is now daemonized */ }
Err(e) => {
eprintln!("Error daemonizing: {}", e);
return; // Exit if daemonization fails
}
}
}
// 🔄 Ensure there's always a config.yml present
ensure_config();