Struct rexpect::process::PtyProcess
source · [−]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):
extern crate nix;
extern crate rexpect;
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.as_raw_fd()).unwrap();
let f = unsafe { File::from_raw_fd(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
sourceimpl PtyProcess
impl PtyProcess
sourcepub fn get_file_handle(&self) -> File
pub fn get_file_handle(&self) -> File
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.
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.