Add Monitoring to Rust CLI Tools Run by Cron
RustThe Problem
Rust CLI tools built with clap or structopt run as cron jobs need external verification that they completed successfully.
The Solution
Add a monitoring flag or built-in ping to your CLI tool that reports status to CronRabbit.
Integration Approach
Accept the ping URL as a command-line argument or environment variable. This keeps your binary reusable while enabling monitoring when run from cron.
Code Examples
CLI with optional monitoring
Rustuse std::env;
fn main() {
let ping_url = env::var("CRONRABBIT_PING").ok();
if let Some(ref url) = ping_url {
let _ = ureq::get(&format!("{url}/start")).call();
}
let result = run_etl();
if let Some(ref url) = ping_url {
match result {
Ok(_) => { let _ = ureq::get(url).call(); }
Err(_) => { let _ = ureq::get(&format!("{url}/fail")).call(); }
}
}
if let Err(e) = result {
eprintln!("Error: {e}");
std::process::exit(1);
}
}