pub struct PtyProcess {
pub pty: PtyMaster,
pub child_pid: Pid,
/* private fields */
}
Expand description
Start a process in a forked tty so you can interact with it the same as you would within a terminal
The process and pty session are killed upon dropping PtyProcess
§Example
Typically you want to do something like this (for a more complete example see
unit test test_cat
within this module):
use rexpect::process::PtyProcess;
use std::process::Command;
use std::fs::File;
use std::io::{BufReader, LineWriter};
use std::os::unix::io::{FromRawFd, AsRawFd};
use nix::unistd::dup;
let mut process = PtyProcess::new(Command::new("cat")).expect("could not execute cat");
let fd = dup(&process.pty).unwrap();
let f = File::from(fd);
let mut writer = LineWriter::new(&f);
let mut reader = BufReader::new(&f);
process.exit().expect("could not terminate process");
// writer.write() sends strings to `cat`
// writer.reader() reads back what `cat` wrote
// send Ctrl-C with writer.write(&[3]) and writer.flush()
Fields§
§pty: PtyMaster
§child_pid: Pid
Implementations§
Source§impl PtyProcess
impl PtyProcess
Sourcepub fn get_file_handle(&self) -> Result<File, Error>
pub fn get_file_handle(&self) -> Result<File, Error>
Get handle to pty fork for reading/writing
Sourcepub fn set_kill_timeout(&mut self, timeout_ms: Option<u64>)
pub fn set_kill_timeout(&mut self, timeout_ms: Option<u64>)
At the drop of PtyProcess
the running process is killed. This is blocking forever if
the process does not react to a normal kill. If kill_timeout
is set the process is
kill -9
ed after duration
Sourcepub fn status(&self) -> Option<WaitStatus>
pub fn status(&self) -> Option<WaitStatus>
Get status of child process, non-blocking.
This method runs waitpid on the process.
This means: If you ran exit()
before or status()
this method will
return None
§Example
use rexpect::process::{self, wait::WaitStatus};
use std::process::Command;
let cmd = Command::new("/path/to/myprog");
let process = process::PtyProcess::new(cmd).expect("could not execute myprog");
while let Some(WaitStatus::StillAlive) = process.status() {
// do something
}
Sourcepub fn wait(&self) -> Result<WaitStatus, Error>
pub fn wait(&self) -> Result<WaitStatus, Error>
Wait until process has exited. This is a blocking call. If the process doesn’t terminate this will block forever.
Examples found in repository?
9fn main() -> Result<(), Error> {
10 let p = spawn("cat /etc/passwd", Some(2000))?;
11 match p.process.wait() {
12 Ok(wait::WaitStatus::Exited(_, 0)) => println!("cat exited with code 0, all good!"),
13 _ => println!("cat exited with code >0, or it was killed"),
14 }
15
16 let mut p = spawn("cat /this/does/not/exist", Some(2000))?;
17 match p.process.wait() {
18 Ok(wait::WaitStatus::Exited(_, 0)) => println!("cat succeeded"),
19 Ok(wait::WaitStatus::Exited(_, c)) => {
20 println!("Cat failed with exit code {c}");
21 println!("Output (stdout and stderr): {}", p.exp_eof()?);
22 }
23 // for other possible return types of wait()
24 // see here: https://tailhook.github.io/rotor/nix/sys/wait/enum.WaitStatus.html
25 _ => println!("cat was probably killed"),
26 }
27
28 Ok(())
29}
Sourcepub fn exit(&mut self) -> Result<WaitStatus, Error>
pub fn exit(&mut self) -> Result<WaitStatus, Error>
Regularly exit the process, this method is blocking until the process is dead
Sourcepub fn signal(&mut self, sig: Signal) -> Result<(), Error>
pub fn signal(&mut self, sig: Signal) -> Result<(), Error>
Non-blocking variant of kill()
(doesn’t wait for process to be killed)
Sourcepub fn kill(&mut self, sig: Signal) -> Result<WaitStatus, Error>
pub fn kill(&mut self, sig: Signal) -> Result<WaitStatus, Error>
Kill the process with a specific signal. This method blocks, until the process is dead
repeatedly sends SIGTERM to the process until it died,
the pty session is closed upon dropping PtyMaster
,
so we don’t need to explicitly do that here.
if kill_timeout
is set and a repeated sending of signal does not result in the process
being killed, then kill -9
is sent after the kill_timeout
duration has elapsed.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for PtyProcess
impl RefUnwindSafe for PtyProcess
impl Send for PtyProcess
impl Sync for PtyProcess
impl Unpin for PtyProcess
impl UnwindSafe for PtyProcess
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> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more