nushell/crates/nu-command/src/system/ps.rs

207 lines
6.9 KiB
Rust
Raw Normal View History

add more `ps` columns in Windows (#10275) # Description This PR adds a few more columns to `ps -l` on Windows. It would be good to add these changes cross-platform in separate PRs. This PR also fixes a bug where start time was calculated wrong. I've added: start_time user user_sid priority ![image](https://github.com/nushell/nushell/assets/343840/cba16a17-ee70-46b5-9e6d-ef06641b264e) # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass (on Windows make sure to [enable developer mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging)) - `cargo run -- -c "use std testing; testing run-tests --path crates/nu-std"` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. -->
2023-09-08 17:24:29 +00:00
#[cfg(windows)]
use itertools::Itertools;
add a few more columns to linux `ps -l` output (#10344) # Description This PR tried to add a few more columns to the Linux `ps -l` command. Those columns are: * start_time * user_id * priority * process_threads There are a few that I left commented out that could be added but the screen was beginning to look crowded. So, I left out: * group_id * session_id * tgp_id (which could be helpful for eventual job control) And there's like 100 more things that could be added that didn't seem especially useful right now. ![image](https://github.com/nushell/nushell/assets/343840/065c0538-8f7d-4c9f-871f-a1bc98aff9d1) # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass (on Windows make sure to [enable developer mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging)) - `cargo run -- -c "use std testing; testing run-tests --path crates/nu-std"` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. -->
2023-09-12 19:50:05 +00:00
#[cfg(all(
unix,
not(target_os = "macos"),
not(target_os = "windows"),
not(target_os = "android"),
not(target_os = "ios")
))]
use nu_protocol::Span;
2021-10-01 21:53:13 +00:00
use nu_protocol::{
ast::Call,
2021-10-25 16:58:58 +00:00
engine::{Command, EngineState, Stack},
Category, Example, IntoInterruptiblePipelineData, PipelineData, Record, ShellError, Signature,
Type, Value,
2021-10-01 21:53:13 +00:00
};
#[cfg(all(
unix,
not(target_os = "macos"),
not(target_os = "windows"),
not(target_os = "android"),
not(target_os = "ios")
))]
use procfs::WithCurrentSystemInfo;
add a few more columns to linux `ps -l` output (#10344) # Description This PR tried to add a few more columns to the Linux `ps -l` command. Those columns are: * start_time * user_id * priority * process_threads There are a few that I left commented out that could be added but the screen was beginning to look crowded. So, I left out: * group_id * session_id * tgp_id (which could be helpful for eventual job control) And there's like 100 more things that could be added that didn't seem especially useful right now. ![image](https://github.com/nushell/nushell/assets/343840/065c0538-8f7d-4c9f-871f-a1bc98aff9d1) # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass (on Windows make sure to [enable developer mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging)) - `cargo run -- -c "use std testing; testing run-tests --path crates/nu-std"` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. -->
2023-09-12 19:50:05 +00:00
add more `ps` columns in Windows (#10275) # Description This PR adds a few more columns to `ps -l` on Windows. It would be good to add these changes cross-platform in separate PRs. This PR also fixes a bug where start time was calculated wrong. I've added: start_time user user_sid priority ![image](https://github.com/nushell/nushell/assets/343840/cba16a17-ee70-46b5-9e6d-ef06641b264e) # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass (on Windows make sure to [enable developer mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging)) - `cargo run -- -c "use std testing; testing run-tests --path crates/nu-std"` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. -->
2023-09-08 17:24:29 +00:00
use std::time::Duration;
2021-10-01 21:53:13 +00:00
2021-10-25 04:01:02 +00:00
#[derive(Clone)]
2021-10-01 21:53:13 +00:00
pub struct Ps;
impl Command for Ps {
fn name(&self) -> &str {
"ps"
}
fn signature(&self) -> Signature {
Signature::build("ps")
.input_output_types(vec![(Type::Nothing, Type::Table(vec![]))])
2021-10-01 21:53:13 +00:00
.switch(
"long",
"list all available columns for each entry",
Some('l'),
)
.filter()
.category(Category::System)
2021-10-01 21:53:13 +00:00
}
fn usage(&self) -> &str {
"View information about system processes."
}
fn search_terms(&self) -> Vec<&str> {
vec!["procedures", "operations", "tasks", "ops"]
}
2021-10-01 21:53:13 +00:00
fn run(
&self,
2021-10-28 04:13:10 +00:00
engine_state: &EngineState,
2021-10-25 06:31:39 +00:00
_stack: &mut Stack,
2021-10-01 21:53:13 +00:00
call: &Call,
2021-10-25 04:01:02 +00:00
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
2021-10-28 04:13:10 +00:00
run_ps(engine_state, call)
2021-10-01 21:53:13 +00:00
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "List the system processes",
example: "ps",
result: None,
},
Example {
description: "List the top 5 system processes with the highest memory usage",
example: "ps | sort-by mem | last 5",
result: None,
},
Example {
description: "List the top 3 system processes with the highest CPU usage",
example: "ps | sort-by cpu | last 3",
result: None,
},
Example {
description: "List the system processes with 'nu' in their names",
example: "ps | where name =~ 'nu'",
result: None,
},
Example {
description: "Get the parent process id of the current nu process",
example: "ps | where pid == $nu.pid | get ppid",
result: None,
},
]
2021-10-01 21:53:13 +00:00
}
}
2021-10-28 04:13:10 +00:00
fn run_ps(engine_state: &EngineState, call: &Call) -> Result<PipelineData, ShellError> {
let mut output = vec![];
2021-10-01 21:53:13 +00:00
let span = call.head;
let long = call.has_flag("long");
for proc in nu_system::collect_proc(Duration::from_millis(100), false) {
let mut record = Record::new();
record.push("pid", Value::int(proc.pid() as i64, span));
record.push("ppid", Value::int(proc.ppid() as i64, span));
record.push("name", Value::string(proc.name(), span));
#[cfg(not(windows))]
{
// Hide status on Windows until we can find a good way to support it
record.push("status", Value::string(proc.status(), span));
}
record.push("cpu", Value::float(proc.cpu_usage(), span));
record.push("mem", Value::filesize(proc.mem_size() as i64, span));
record.push("virtual", Value::filesize(proc.virtual_size() as i64, span));
if long {
record.push("command", Value::string(proc.command(), span));
add a few more columns to linux `ps -l` output (#10344) # Description This PR tried to add a few more columns to the Linux `ps -l` command. Those columns are: * start_time * user_id * priority * process_threads There are a few that I left commented out that could be added but the screen was beginning to look crowded. So, I left out: * group_id * session_id * tgp_id (which could be helpful for eventual job control) And there's like 100 more things that could be added that didn't seem especially useful right now. ![image](https://github.com/nushell/nushell/assets/343840/065c0538-8f7d-4c9f-871f-a1bc98aff9d1) # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass (on Windows make sure to [enable developer mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging)) - `cargo run -- -c "use std testing; testing run-tests --path crates/nu-std"` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. -->
2023-09-12 19:50:05 +00:00
#[cfg(all(
unix,
not(target_os = "macos"),
not(target_os = "windows"),
not(target_os = "android"),
not(target_os = "ios")
))]
{
let proc_stat = proc.curr_proc.stat().map_err(|e| {
ShellError::GenericError(
"Error getting process stat".into(),
e.to_string(),
Some(Span::unknown()),
None,
Vec::new(),
)
})?;
// If we can't get the start time, just use the current time
let proc_start = proc_stat
.starttime()
.get()
.unwrap_or_else(|_| chrono::Local::now());
add a few more columns to linux `ps -l` output (#10344) # Description This PR tried to add a few more columns to the Linux `ps -l` command. Those columns are: * start_time * user_id * priority * process_threads There are a few that I left commented out that could be added but the screen was beginning to look crowded. So, I left out: * group_id * session_id * tgp_id (which could be helpful for eventual job control) And there's like 100 more things that could be added that didn't seem especially useful right now. ![image](https://github.com/nushell/nushell/assets/343840/065c0538-8f7d-4c9f-871f-a1bc98aff9d1) # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass (on Windows make sure to [enable developer mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging)) - `cargo run -- -c "use std testing; testing run-tests --path crates/nu-std"` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. -->
2023-09-12 19:50:05 +00:00
record.push("start_time", Value::date(proc_start.into(), span));
record.push("user_id", Value::int(proc.curr_proc.owner() as i64, span));
// These work and may be helpful, but it just seemed crowded
// record.push("group_id", Value::int(proc_stat.pgrp as i64, span));
// record.push("session_id", Value::int(proc_stat.session as i64, span));
// This may be helpful for ctrl+z type of checking, once we get there
// record.push("tpg_id", Value::int(proc_stat.tpgid as i64, span));
record.push("priority", Value::int(proc_stat.priority, span));
record.push("process_threads", Value::int(proc_stat.num_threads, span));
record.push("cwd", Value::string(proc.cwd(), span));
add a few more columns to linux `ps -l` output (#10344) # Description This PR tried to add a few more columns to the Linux `ps -l` command. Those columns are: * start_time * user_id * priority * process_threads There are a few that I left commented out that could be added but the screen was beginning to look crowded. So, I left out: * group_id * session_id * tgp_id (which could be helpful for eventual job control) And there's like 100 more things that could be added that didn't seem especially useful right now. ![image](https://github.com/nushell/nushell/assets/343840/065c0538-8f7d-4c9f-871f-a1bc98aff9d1) # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass (on Windows make sure to [enable developer mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging)) - `cargo run -- -c "use std testing; testing run-tests --path crates/nu-std"` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. -->
2023-09-12 19:50:05 +00:00
}
#[cfg(windows)]
{
add more `ps` columns in Windows (#10275) # Description This PR adds a few more columns to `ps -l` on Windows. It would be good to add these changes cross-platform in separate PRs. This PR also fixes a bug where start time was calculated wrong. I've added: start_time user user_sid priority ![image](https://github.com/nushell/nushell/assets/343840/cba16a17-ee70-46b5-9e6d-ef06641b264e) # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass (on Windows make sure to [enable developer mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging)) - `cargo run -- -c "use std testing; testing run-tests --path crates/nu-std"` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. -->
2023-09-08 17:24:29 +00:00
//TODO: There's still more information we can cram in there if we want to
// see the ProcessInfo struct for more information
record.push(
"start_time",
Value::date(proc.start_time.fixed_offset(), span),
);
record.push(
"user",
Value::string(
proc.user.clone().name.unwrap_or("unknown".to_string()),
span,
),
);
record.push(
"user_sid",
Value::string(
proc.user
.clone()
.sid
.iter()
.map(|r| r.to_string())
.join("-"),
span,
),
);
record.push("priority", Value::int(proc.priority as i64, span));
record.push("cwd", Value::string(proc.cwd(), span));
record.push(
"environment",
Value::list(
proc.environ()
.iter()
.map(|x| Value::string(x.to_string(), span))
.collect(),
span,
),
);
}
#[cfg(target_os = "macos")]
{
record.push("cwd", Value::string(proc.cwd(), span));
}
2021-10-01 21:53:13 +00:00
}
output.push(Value::record(record, span));
2021-10-01 21:53:13 +00:00
}
2021-10-28 04:13:10 +00:00
Ok(output
.into_iter()
.into_pipeline_data(engine_state.ctrlc.clone()))
2021-10-01 21:53:13 +00:00
}