Struct command_group::AsyncGroupChild [−][src]
pub struct AsyncGroupChild { /* fields omitted */ }
Expand description
Representation of a running or exited child process group (Tokio variant).
This wraps Tokio’s Child
type with methods that work with process groups.
Examples
use tokio::process::Command;
use command_group::AsyncCommandGroup;
let mut child = Command::new("/bin/cat")
.arg("file.txt")
.group_spawn()
.expect("failed to execute child");
let ecode = child.wait()
.await
.expect("failed to wait on child");
assert!(ecode.success());
Implementations
Returns the stdlib Child
object.
Note that the inner child may not be in the same state as this output child, due to how
methods like wait
and kill
are implemented. It is not recommended to use this method
after using any of the other methods on this struct.
Examples
Reading from stdout:
use std::process::Stdio;
use tokio::{io::AsyncReadExt, process::Command};
use command_group::AsyncCommandGroup;
let mut child = Command::new("ls").stdout(Stdio::piped()).group_spawn().expect("ls command didn't start");
let mut output = String::new();
if let Some(mut out) = child.inner().stdout.take() {
out.read_to_string(&mut output).await.expect("failed to read from child");
}
println!("output: {}", output);
Consumes itself and returns the stdlib Child
object.
Note that the inner child may not be in the same state as this output child, due to how
methods like wait
and kill
are implemented. It is not recommended to use this method
after using any of the other methods on this struct.
Examples
Writing to input:
use std::process::Stdio;
use tokio::{io::AsyncWriteExt, process::Command};
use command_group::AsyncCommandGroup;
let mut child = Command::new("cat").stdin(Stdio::piped()).group_spawn().expect("cat command didn't start");
if let Some(mut din) = child.into_inner().stdin.take() {
din.write_all(b"Woohoo!").await.expect("failed to write");
}
Forces the child process group to exit. If the group has already exited, an InvalidInput
error is returned.
This is equivalent to sending a SIGKILL on Unix platforms.
Unlike the Tokio implementation, this method does not wait for the child process group,
and only sends the kill. You’ll need to call wait()
yourself.
See the Tokio documentation for more.
Examples
Basic usage:
use tokio::process::Command;
use command_group::AsyncCommandGroup;
let mut command = Command::new("yes");
if let Ok(mut child) = command.group_spawn() {
child.kill().expect("command wasn't running");
} else {
println!("yes command didn't start");
}
Returns the OS-assigned process group identifier.
Like Tokio, this returns None
if the child process group has alread exited, to avoid
holding onto an expired (and possibly reused) PGID.
See the Tokio documentation for more.
Examples
Basic usage:
use tokio::process::Command;
use command_group::AsyncCommandGroup;
let mut command = Command::new("ls");
if let Ok(child) = command.group_spawn() {
if let Some(pgid) = child.id() {
println!("Child group's ID is {}", pgid);
} else {
println!("Child group is gone");
}
} else {
println!("ls command didn't start");
}
Waits for the child group to exit completely, returning the status that the process leader exited with.
See the Tokio documentation for more.
The current implementation spawns a blocking task on the Tokio thread pool; contributions are welcome for a more async-y version.
Examples
Basic usage:
use tokio::process::Command;
use command_group::AsyncCommandGroup;
let mut command = Command::new("ls");
if let Ok(mut child) = command.group_spawn() {
child.wait().await.expect("command wasn't running");
println!("Child has finished its execution!");
} else {
println!("ls command didn't start");
}
Attempts to collect the exit status of the child if it has already exited.
See the Tokio documentation for more.
Examples
Basic usage:
use tokio::process::Command;
use command_group::AsyncCommandGroup;
let mut child = Command::new("ls").group_spawn().unwrap();
match child.try_wait() {
Ok(Some(status)) => println!("exited with: {}", status),
Ok(None) => {
println!("status not ready yet, let's really wait");
let res = child.wait().await;
println!("result: {:?}", res);
}
Err(e) => println!("error attempting to wait: {}", e),
}
Simultaneously waits for the child to exit and collect all remaining output on the
stdout/stderr handles, returning an Output
instance.
See the Tokio documentation for more.
Examples
Basic usage:
use std::process::Stdio;
use tokio::process::Command;
use command_group::AsyncCommandGroup;
let child = Command::new("/bin/cat")
.arg("file.txt")
.stdout(Stdio::piped())
.group_spawn()
.expect("failed to execute child");
let output = child
.wait_with_output()
.await
.expect("failed to wait on child");
assert!(output.status.success());