missed file

This commit is contained in:
Nickiel 2024-12-25 19:17:34 -08:00
parent e43ce2a79d
commit 100902a569

42
src/process_management.rs Normal file
View file

@ -0,0 +1,42 @@
use log::{info, error};
use std::process::Command;
/// Stops existing versions of the program with pkill term commands
pub fn stop_existing() {
let main_process_std_out =
match Command::new("ps").arg("aux").output() {
Ok(val) => val,
Err(e) => {
error!("Error getting existing programs: {e}");
panic!("Could not get currently running programs");
}
}.stdout;
let this_id = std::process::id().to_string();
let parse_str = String::from_utf8_lossy(&main_process_std_out);
let existing_program: Vec<_> = parse_str
.split('\n')
.filter(|x| x.contains("vcs-camera-satellite") && !x.contains(&this_id))
.collect();
if existing_program.len() > 0 {
let list_ids = existing_program.iter().map(|x| {
if let Some(first_num) = x.split_whitespace().collect::<Vec<_>>().get(1) {
if let Ok(num) = first_num.parse::<i32>() {
return num;
}
}
return 0;
}).filter(|x| x > &0).collect::<Vec<i32>>();
for id in list_ids.iter() {
info!("trying to kill: {}", id);
_ = Command::new("kill").arg("-9").arg(format!("{}", id)).output();
}
info!("Another instance of this program found. Attempting to kill it");
// _ = Command::new("pkill").arg("-f").arg("vcs-camera-satellite").output();
}
}