From 100902a5698daaa1e165a46a292afb82bd137cc0 Mon Sep 17 00:00:00 2001 From: Nickiel Date: Wed, 25 Dec 2024 19:17:34 -0800 Subject: [PATCH] missed file --- src/process_management.rs | 42 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/process_management.rs diff --git a/src/process_management.rs b/src/process_management.rs new file mode 100644 index 0000000..7dd278e --- /dev/null +++ b/src/process_management.rs @@ -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::>().get(1) { + if let Ok(num) = first_num.parse::() { + return num; + } + } + return 0; + }).filter(|x| x > &0).collect::>(); + + 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(); + } + +} +