diff --git a/src/uu/pgrep/src/process_matcher.rs b/src/uu/pgrep/src/process_matcher.rs index c24c13a..460e947 100644 --- a/src/uu/pgrep/src/process_matcher.rs +++ b/src/uu/pgrep/src/process_matcher.rs @@ -168,7 +168,12 @@ fn collect_matched_pids(settings: &Settings) -> Vec { let filtered: Vec = { let mut tmp_vec = Vec::new(); + let our_pid = std::process::id() as usize; for mut pid in walk_process().collect::>() { + if pid.pid == our_pid { + continue; + } + let run_state_matched = match (&settings.runstates, pid.run_state()) { (Some(arg_run_states), Ok(pid_state)) => { arg_run_states.contains(&pid_state.to_string()) diff --git a/tests/by-util/test_pgrep.rs b/tests/by-util/test_pgrep.rs index 55cb127..303ce8d 100644 --- a/tests/by-util/test_pgrep.rs +++ b/tests/by-util/test_pgrep.rs @@ -420,3 +420,15 @@ fn test_current_user() { .arg(uucore::process::getuid().to_string()) .succeeds(); } + +#[test] +#[cfg(target_os = "linux")] +fn test_does_not_match_current_process() { + let our_pid = std::process::id(); + dbg!(&our_pid); + new_ucmd!() + .arg("-f") + .arg("UNIQUE_STRING_THAT_DOES_NOT_MATCH_ANY_OTHER_PROCESS") + .fails() + .no_output(); +}