Structure to handle lifetime and communications with child process
Buffered timeout reader pointed to stdout pipe
Buffered timeout reader pointed to stderr pipe
Spawn a process and setup handler structure
- command - Path to binary to execute
- args - Optional arguments for binary
use shell_protocol::*;
let proc = ProcessHandler::spawn(&"/bin/bash".to_owned(), None);
use shell_protocol::*;
let proc = ProcessHandler::spawn(&"ls".to_owned(), Some(vec!["-l".to_owned()]));
Attempt to read from stdout
A return value of None
indicates the stream is
no longer available and likewise the process
is likely no longer alive.
use shell_protocol::*;
let mut proc = ProcessHandler::spawn(&"ls".to_owned(), None).unwrap();
match proc.read_stdout() {
Ok(Some(output)) => println!("Stdout: {}", output),
Ok(None) => println!("Stdout time out"),
Err(e) => eprintln!("Stdout err {}", e),
}
Attempt to read from stderr
A return value of None
indicates the stream is
no longer available and likewise the process
is likely no longer alive.
use shell_protocol::*;
let mut proc = ProcessHandler::spawn(&"ls".to_owned(), None).unwrap();
match proc.read_stderr() {
Ok(Some(output)) => println!("Stderr: {}", output),
Ok(None) => println!("Stderr time out"),
Err(e) => eprintln!("Stderr err {}", e),
}
Attempt to write to stdin
- data - Slice of bytes to write
use shell_protocol::*;
let cmd = "ls\n".as_bytes();
let mut proc = ProcessHandler::spawn(&"/bin/bash".to_owned(), None).unwrap();
match proc.write_stdin(&cmd) {
Ok(()) => println!("Stdin write success"),
Err(e) => eprintln!("Stdin err {}", e),
}
Close process' stdin pipe
use shell_protocol::*;
let mut proc = ProcessHandler::spawn(&"/bin/bash".to_owned(), None).unwrap();
match proc.close_stdin() {
Ok(()) => println!("Stdin closed"),
Err(e) => eprintln!("Stdin close err {}", e),
}
Retrieve ID of process
use shell_protocol::*;
let proc = ProcessHandler::spawn(&"/bin/bash".to_owned(), None).unwrap();
let pid = proc.id();
Check to see if a process has exited and if the exit
status is available
use shell_protocol::*;
let mut proc = ProcessHandler::spawn(&"/bin/bash".to_owned(), None).unwrap();
match proc.status() {
Ok(Some((code, signal))) => println!("Process has exited: {}, {}", code, signal),
Ok(None) => println!("Process has not exited"),
Err(e) => eprintln!("Error getting process status {}", e)
}
Send killing signal to process
- signal - Optional signal to send to process
use shell_protocol::*;
let mut proc = ProcessHandler::spawn(&"/bin/bash".to_owned(), None).unwrap();
match proc.kill(None) {
Ok(()) => println!("Process killed"),
Err(e) => eprintln!("Error killing process: {}", e),
}
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static