This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

at main 2.0 kB View raw
1use std::process::Command; 2use std::env; 3use std::io::Write; 4use tracing::{info, error}; 5 6pub fn setup_cron_job() { 7 let current_exe = match env::current_exe() { 8 Ok(path) => path, 9 Err(e) => { 10 error!("Failed to get current exe path: {}", e); 11 return; 12 } 13 }; 14 let current_exe_str = current_exe.to_str().expect("Failed to convert path to string"); 15 16 // Command to check if cron job exists 17 let output = Command::new("crontab") 18 .arg("-l") 19 .output(); 20 21 let cron_list = match output { 22 Ok(out) if out.status.success() => String::from_utf8_lossy(&out.stdout).to_string(), 23 _ => String::new(), 24 }; 25 26 if cron_list.contains(current_exe_str) { 27 info!("Cron job already exists."); 28 return; 29 } 30 31 let new_job = format!("0 * * * * {}", current_exe_str); 32 let mut new_cron = cron_list; 33 if !new_cron.is_empty() && !new_cron.ends_with('\n') { 34 new_cron.push('\n'); 35 } 36 new_cron.push_str(&new_job); 37 new_cron.push('\n'); 38 39 let child = Command::new("crontab") 40 .arg("-") 41 .stdin(std::process::Stdio::piped()) 42 .spawn(); 43 44 match child { 45 Ok(mut child) => { 46 let mut stdin = child.stdin.take().expect("Failed to open stdin"); 47 if let Err(e) = stdin.write_all(new_cron.as_bytes()) { 48 error!("Failed to write to crontab stdin: {}", e); 49 return; 50 } 51 drop(stdin); 52 53 match child.wait() { 54 Ok(status) => { 55 if status.success() { 56 info!("Cron job has been set up to run every hour."); 57 } else { 58 error!("Failed to set up cron job (crontab exited with error)."); 59 } 60 } 61 Err(e) => error!("Failed to wait on crontab process: {}", e), 62 } 63 } 64 Err(e) => error!("Failed to spawn crontab process: {}", e), 65 } 66}