webterm_agent/
daemonise.rs1use crate::models::panic_error::PanicError;
2use std::fs::OpenOptions;
3
4pub fn daemonise() -> Result<(), PanicError> {
5 use daemonize::Daemonize;
6
7 let log_path = "/tmp/webterm-agent.log";
8
9 let stdout = OpenOptions::new()
10 .create(true)
11 .append(true)
12 .open(log_path)
13 .map_err(|_| {
14 PanicError::RuntimeError(format!("Could not create stdout log file: {}", log_path))
15 })?;
16
17 let stderr = stdout.try_clone().map_err(|_| {
18 PanicError::RuntimeError(format!("Could not create stderr log file: {}", log_path))
19 })?;
20
21 let daemonize = Daemonize::new()
22 .pid_file("/tmp/webterm-agent.pid")
23 .stdout(stdout)
24 .stderr(stderr);
25
26 println!("Running in background, logging to {}", log_path);
27
28 match daemonize.start() {
29 Ok(_) => Ok(()),
30 Err(e) => {
31 eprintln!("Failed to run in background: {}", e);
32 Err(PanicError::RuntimeError(e.to_string()))
33 }
34 }
35}