rust-cli / rexpect

.github/workflows/ci.yml
https://docs.rs/rexpect
MIT License
328 stars 56 forks source link

Send signals to underlying process? #1

Closed Michael-F-Bryan closed 7 years ago

Michael-F-Bryan commented 7 years ago

I notice you can send a control character like <ctrl-c> for keyboard interrupt, or <ctrl-d> for EOF. Is there any way to send a signal like SIGTERM or SIGHUP (perhaps using a nix::sys::signal::Signal) to the underlying process?

An alternative might be adding a method to the PtySession which gives you a mutable reference to the underlying PtyProcess so you can call signal() and friends directly on that.

philippkeller commented 7 years ago

Hi Michael. Sorry for the delay, just got back from my holidays (without internet ;-)), I'll look into this in the next days.

philippkeller commented 7 years ago

Ah, the members of PtySession need to be public. Totally forgot about that. I now changed that and pushed to crates.io, so you can do now e.g. this:

extern crate rexpect;

use rexpect::spawn;
use rexpect::process::signal;
use rexpect::errors::*;

fn do_sleep() -> Result<()> {
    let mut p = spawn("sleep 999", Some(2000))?;
    p.process.signal(signal::SIGINT)?;
    Ok(())
}

fn main() {
    do_sleep().unwrap_or_else(|e| panic!("ftp job failed with {}", e));
}
philippkeller commented 7 years ago

Closing this as I believe @Michael-F-Bryan your issue is fixed, right?

Michael-F-Bryan commented 7 years ago

Yep. Sorry, I probably should have closed this as solved once I saw your reply about pushing a fix to crates.io.

philippkeller commented 7 years ago

no worries. I'm glad there are even people using this crate :-)