2020-04-27 04:04:54 +02:00
|
|
|
use crate::commands::WholeStreamCommand;
|
2020-02-01 19:46:28 +01:00
|
|
|
use crate::context::CommandRegistry;
|
|
|
|
use crate::prelude::*;
|
|
|
|
use nu_errors::ShellError;
|
2020-05-16 05:18:24 +02:00
|
|
|
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue};
|
2020-02-01 19:46:28 +01:00
|
|
|
use nu_source::Tagged;
|
|
|
|
use std::process::{Command, Stdio};
|
|
|
|
|
|
|
|
pub struct Kill;
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct KillArgs {
|
|
|
|
pub pid: Tagged<u64>,
|
|
|
|
pub rest: Vec<Tagged<u64>>,
|
|
|
|
pub force: Tagged<bool>,
|
|
|
|
pub quiet: Tagged<bool>,
|
|
|
|
}
|
|
|
|
|
2020-05-29 10:22:52 +02:00
|
|
|
#[async_trait]
|
2020-04-27 04:04:54 +02:00
|
|
|
impl WholeStreamCommand for Kill {
|
2020-02-01 19:46:28 +01:00
|
|
|
fn name(&self) -> &str {
|
|
|
|
"kill"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("kill")
|
|
|
|
.required(
|
|
|
|
"pid",
|
|
|
|
SyntaxShape::Int,
|
|
|
|
"process id of process that is to be killed",
|
|
|
|
)
|
|
|
|
.rest(SyntaxShape::Int, "rest of processes to kill")
|
2020-02-12 03:24:31 +01:00
|
|
|
.switch("force", "forcefully kill the process", Some('f'))
|
|
|
|
.switch("quiet", "won't print anything to the console", Some('q'))
|
2020-02-01 19:46:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Kill a process using the process id."
|
|
|
|
}
|
|
|
|
|
2020-05-29 10:22:52 +02:00
|
|
|
async fn run(
|
2020-02-01 19:46:28 +01:00
|
|
|
&self,
|
2020-04-27 04:04:54 +02:00
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
2020-02-01 19:46:28 +01:00
|
|
|
) -> Result<OutputStream, ShellError> {
|
2020-05-16 05:18:24 +02:00
|
|
|
kill(args, registry)
|
2020-02-01 19:46:28 +01:00
|
|
|
}
|
2020-05-12 17:54:29 +02:00
|
|
|
|
2020-05-18 14:56:01 +02:00
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
vec![
|
2020-05-12 17:54:29 +02:00
|
|
|
Example {
|
|
|
|
description: "Kill the pid using the most memory",
|
|
|
|
example: "ps | sort-by mem | last | kill $it.pid",
|
2020-05-18 14:56:01 +02:00
|
|
|
result: None,
|
2020-05-12 17:54:29 +02:00
|
|
|
},
|
|
|
|
Example {
|
|
|
|
description: "Force kill a given pid",
|
|
|
|
example: "kill --force 12345",
|
2020-05-18 14:56:01 +02:00
|
|
|
result: None,
|
2020-05-12 17:54:29 +02:00
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
2020-02-01 19:46:28 +01:00
|
|
|
}
|
|
|
|
|
2020-05-16 05:18:24 +02:00
|
|
|
fn kill(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
|
|
|
let registry = registry.clone();
|
|
|
|
|
|
|
|
let stream = async_stream! {
|
|
|
|
let (KillArgs {
|
|
|
|
pid,
|
|
|
|
rest,
|
|
|
|
force,
|
|
|
|
quiet,
|
|
|
|
}, mut input) = args.process(®istry).await?;
|
|
|
|
let mut cmd = if cfg!(windows) {
|
|
|
|
let mut cmd = Command::new("taskkill");
|
2020-02-01 19:46:28 +01:00
|
|
|
|
2020-05-16 05:18:24 +02:00
|
|
|
if *force {
|
|
|
|
cmd.arg("/F");
|
|
|
|
}
|
2020-02-01 19:46:28 +01:00
|
|
|
|
|
|
|
cmd.arg("/PID");
|
2020-05-16 05:18:24 +02:00
|
|
|
cmd.arg(pid.item().to_string());
|
2020-02-01 19:46:28 +01:00
|
|
|
|
2020-05-16 05:18:24 +02:00
|
|
|
// each pid must written as `/PID 0` otherwise
|
|
|
|
// taskkill will act as `killall` unix command
|
|
|
|
for id in &rest {
|
|
|
|
cmd.arg("/PID");
|
|
|
|
cmd.arg(id.item().to_string());
|
|
|
|
}
|
2020-02-01 19:46:28 +01:00
|
|
|
|
2020-05-16 05:18:24 +02:00
|
|
|
cmd
|
|
|
|
} else {
|
|
|
|
let mut cmd = Command::new("kill");
|
2020-02-01 19:46:28 +01:00
|
|
|
|
2020-05-16 05:18:24 +02:00
|
|
|
if *force {
|
|
|
|
cmd.arg("-9");
|
|
|
|
}
|
2020-02-01 19:46:28 +01:00
|
|
|
|
2020-05-16 05:18:24 +02:00
|
|
|
cmd.arg(pid.item().to_string());
|
2020-02-01 19:46:28 +01:00
|
|
|
|
2020-05-16 05:18:24 +02:00
|
|
|
cmd.args(rest.iter().map(move |id| id.item().to_string()));
|
2020-02-01 19:46:28 +01:00
|
|
|
|
2020-05-16 05:18:24 +02:00
|
|
|
cmd
|
|
|
|
};
|
2020-02-01 19:46:28 +01:00
|
|
|
|
2020-05-16 05:18:24 +02:00
|
|
|
// pipe everything to null
|
|
|
|
if *quiet {
|
|
|
|
cmd.stdin(Stdio::null())
|
|
|
|
.stdout(Stdio::null())
|
|
|
|
.stderr(Stdio::null());
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.status().expect("failed to execute shell command");
|
|
|
|
|
|
|
|
if false {
|
|
|
|
yield ReturnSuccess::value(UntaggedValue::nothing().into_value(Tag::unknown()));
|
|
|
|
}
|
|
|
|
};
|
2020-02-01 19:46:28 +01:00
|
|
|
|
2020-05-16 05:18:24 +02:00
|
|
|
Ok(stream.to_output_stream())
|
2020-02-01 19:46:28 +01:00
|
|
|
}
|
2020-05-18 14:56:01 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::Kill;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn examples_work_as_expected() {
|
|
|
|
use crate::examples::test as test_examples;
|
|
|
|
|
|
|
|
test_examples(Kill {})
|
|
|
|
}
|
|
|
|
}
|