nushell/crates/nu-command/src/platform/kill.rs
Renan Ribeiro 9bb7f0c7dc
Jobs (#14883)
# Description

This is an attempt to improve the nushell situation with regard to issue
#247.

This PR implements:
- [X] spawning jobs: `job spawn { do_background_thing }`
Jobs will be implemented as threads and not forks, to maintain a
consistent behavior between unix and windows.

- [X] listing running jobs: `job list`
This should allow users to list what background tasks they currently
have running.

- [X] killing jobs: `job kill <id>`
- [X] interupting nushell code in the job's background thread
- [X] interrupting the job's currently-running process, if any.

Things that should be taken into consideration for implementation:
- [X] (unix-only) Handling `TSTP` signals while executing code and
turning the current program into a background job, and unfreezing them
in foreground `job unfreeze`.

- [X] Ensuring processes spawned by background jobs get distinct process
groups from the nushell shell itself

This PR originally aimed to implement some of the following, but it is
probably ideal to be left for another PR (scope creep)
- Disowning external process jobs (`job dispatch`)
- Inter job communication (`job send/recv`)

Roadblocks encountered so far:
- Nushell does some weird terminal sequence magics which make so that
when a background process or thread prints something to stderr and the
prompt is idle, the stderr output ends up showing up weirdly
2025-02-25 12:09:52 -05:00

165 lines
5.0 KiB
Rust

use nu_engine::command_prelude::*;
use nu_system::build_kill_command;
use std::process::Stdio;
#[derive(Clone)]
pub struct Kill;
impl Command for Kill {
fn name(&self) -> &str {
"kill"
}
fn description(&self) -> &str {
"Kill a process using the process id."
}
fn signature(&self) -> Signature {
let signature = Signature::build("kill")
.input_output_types(vec![(Type::Nothing, Type::Any)])
.allow_variants_without_examples(true)
.required(
"pid",
SyntaxShape::Int,
"Process id of process that is to be killed.",
)
.rest("rest", SyntaxShape::Int, "Rest of processes to kill.")
.switch("force", "forcefully kill the process", Some('f'))
.switch("quiet", "won't print anything to the console", Some('q'))
.category(Category::Platform);
if cfg!(windows) {
return signature;
}
signature.named(
"signal",
SyntaxShape::Int,
"signal decimal number to be sent instead of the default 15 (unsupported on Windows)",
Some('s'),
)
}
fn search_terms(&self) -> Vec<&str> {
vec!["stop", "end", "close"]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
let pid: i64 = call.req(engine_state, stack, 0)?;
let rest: Vec<i64> = call.rest(engine_state, stack, 1)?;
let force: bool = call.has_flag(engine_state, stack, "force")?;
let signal: Option<Spanned<i64>> = call.get_flag(engine_state, stack, "signal")?;
let quiet: bool = call.has_flag(engine_state, stack, "quiet")?;
if cfg!(unix) {
if let (
true,
Some(Spanned {
item: _,
span: signal_span,
}),
) = (force, signal)
{
return Err(ShellError::IncompatibleParameters {
left_message: "force".to_string(),
left_span: call
.get_flag_span(stack, "force")
.expect("Had flag force, but didn't have span for flag"),
right_message: "signal".to_string(),
right_span: Span::merge(
call.get_flag_span(stack, "signal")
.expect("Had flag signal, but didn't have span for flag"),
signal_span,
),
});
}
};
let mut cmd = build_kill_command(
force,
std::iter::once(pid).chain(rest),
signal.map(|spanned| spanned.item as u32),
);
// pipe everything to null
if quiet {
cmd.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null());
}
let output = cmd.output().map_err(|e| ShellError::GenericError {
error: "failed to execute shell command".into(),
msg: e.to_string(),
span: Some(call.head),
help: None,
inner: vec![],
})?;
if !quiet && !output.status.success() {
return Err(ShellError::GenericError {
error: "process didn't terminate successfully".into(),
msg: String::from_utf8(output.stderr).unwrap_or_default(),
span: Some(call.head),
help: None,
inner: vec![],
});
}
let mut output =
String::from_utf8(output.stdout).map_err(|e| ShellError::GenericError {
error: "failed to convert output to string".into(),
msg: e.to_string(),
span: Some(call.head),
help: None,
inner: vec![],
})?;
output.truncate(output.trim_end().len());
if output.is_empty() {
Ok(Value::nothing(call.head).into_pipeline_data())
} else {
Ok(Value::string(output, call.head).into_pipeline_data())
}
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Kill the pid using the most memory",
example: "ps | sort-by mem | last | kill $in.pid",
result: None,
},
Example {
description: "Force kill a given pid",
example: "kill --force 12345",
result: None,
},
#[cfg(not(target_os = "windows"))]
Example {
description: "Send INT signal",
example: "kill -s 2 12345",
result: None,
},
]
}
}
#[cfg(test)]
mod tests {
use super::Kill;
#[test]
fn examples_work_as_expected() {
use crate::test_examples;
test_examples(Kill {})
}
}