pub struct ProcessRunner { /* private fields */ }Expand description
A running or completed process
Implementations§
Source§impl ProcessRunner
impl ProcessRunner
Sourcepub fn new(command: impl Into<String>, options: RunOptions) -> Self
pub fn new(command: impl Into<String>, options: RunOptions) -> Self
Create a new process runner
Sourcepub fn child(&mut self) -> Option<ProcessChild<'_>>
pub fn child(&mut self) -> Option<ProcessChild<'_>>
Borrow the active operating-system child.
Call start first. The result is None before startup,
for built-in commands (which run in-process), and after run
consumes and reaps the child. Killing through the returned handle keeps
the runner’s process-group and graceful-escalation behavior.
use command_stream::{ProcessRunner, RunOptions};
let mut runner = ProcessRunner::new("sleep 30", RunOptions::default());
runner.start().await?;
if let Some(mut child) = runner.child() {
println!("child pid: {:?}", child.pid());
child.kill_with("SIGTERM")?;
}
let _ = runner.run().await?;Sourcepub async fn write_stdin(&mut self, data: impl AsRef<[u8]>) -> Result<()>
pub async fn write_stdin(&mut self, data: impl AsRef<[u8]>) -> Result<()>
Write bytes to the stdin pipe of a running command.
Configure the runner with StdinOption::Pipe, call start,
write as many chunks as needed, and finish with close_stdin.
Sourcepub async fn close_stdin(&mut self) -> Result<()>
pub async fn close_stdin(&mut self) -> Result<()>
Close a running command’s stdin pipe so it can observe end-of-input.
Sourcepub async fn run(&mut self) -> Result<CommandResult>
pub async fn run(&mut self) -> Result<CommandResult>
Run the process to completion
Sourcepub fn kill(&mut self) -> Result<()>
pub fn kill(&mut self) -> Result<()>
Stop the process using the configured kill signal
(RunOptions::kill_signal, default SIGTERM).
Mirrors the JavaScript kill() with no argument.
Sourcepub fn kill_with(&mut self, signal: &str) -> Result<()>
pub fn kill_with(&mut self, signal: &str) -> Result<()>
Stop the process using an explicit signal, overriding
RunOptions::kill_signal for this call.
Mirrors the JavaScript kill(signal). The signal is delivered to the
child and its process group, so grandchildren behind a sh -c wrapper
are stopped too - except for a child sharing the caller’s terminal,
which stays in the caller’s group so CTRL+C keeps reaching it. The child
then has RunOptions::kill_grace_ms to run its own handler before
SIGKILL follows, so a process that ignores the signal still
terminates.
use command_stream::{ProcessRunner, RunOptions};
let mut runner = ProcessRunner::new("sleep 30", RunOptions::default());
runner.start().await?;
runner.kill_with("SIGINT")?; // the CTRL+C signalSourcepub fn is_finished(&self) -> bool
pub fn is_finished(&self) -> bool
Check if the process is finished
Sourcepub fn result(&self) -> Option<&CommandResult>
pub fn result(&self) -> Option<&CommandResult>
Get the result if available
Sourcepub fn pid(&self) -> Option<u32>
pub fn pid(&self) -> Option<u32>
Process id of the command, or None when there is no operating system
process to identify.
It is None before the command starts, and stays None for built-in
(virtual) commands such as echo or sleep, which run inside this
process and never spawn a child. Once a real command has been spawned
the value is stable: it remains readable after the command finishes,
unlike the child handle, which run consumes.
Mirrors the JavaScript runner.pid property.
use command_stream::{ProcessRunner, RunOptions};
let mut runner = ProcessRunner::new("/bin/sleep 1", RunOptions::default());
runner.start().await?;
println!("running as pid {:?}", runner.pid());
runner.run().await?;
println!("still readable: {:?}", runner.pid());Sourcepub fn options(&self) -> &RunOptions
pub fn options(&self) -> &RunOptions
Get the options
Trait Implementations§
Source§impl IntoStream for ProcessRunner
impl IntoStream for ProcessRunner
Source§fn into_stream(self) -> OutputStream
fn into_stream(self) -> OutputStream
Source§impl PipelineExt for ProcessRunner
impl PipelineExt for ProcessRunner
Auto Trait Implementations§
impl !RefUnwindSafe for ProcessRunner
impl !UnwindSafe for ProcessRunner
impl Freeze for ProcessRunner
impl Send for ProcessRunner
impl Sync for ProcessRunner
impl Unpin for ProcessRunner
impl UnsafeUnpin for ProcessRunner
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.