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):


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: PtyMasterchild_pid: Pid

Implementations

Start a process in a forked pty

Get handle to pty fork for reading/writing

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 -9ed after duration

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
}

Wait until process has exited. This is a blocking call. If the process doesn’t terminate this will block forever.

Regularly exit the process, this method is blocking until the process is dead

Non-blocking variant of kill() (doesn’t wait for process to be killed)

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

Executes the destructor for this type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.