Struct std::old_io::process::ProcessUnstable
[-] [+]
[src]
pub struct Process { pub stdin: Option<PipeStream>, pub stdout: Option<PipeStream>, pub stderr: Option<PipeStream>, // some fields omitted }
Representation of a running or exited child process.
This structure is used to represent and manage child processes. A child
process is created via the Command
struct, which configures the spawning
process and can itself be constructed using a builder-style interface.
Example
fn main() { use std::old_io::Command; let mut child = match Command::new("/bin/cat").arg("file.txt").spawn() { Ok(child) => child, Err(e) => panic!("failed to execute child: {}", e), }; let contents = child.stdout.as_mut().unwrap().read_to_end(); assert!(child.wait().unwrap().success()); }use std::old_io::Command; let mut child = match Command::new("/bin/cat").arg("file.txt").spawn() { Ok(child) => child, Err(e) => panic!("failed to execute child: {}", e), }; let contents = child.stdout.as_mut().unwrap().read_to_end(); assert!(child.wait().unwrap().success());
Fields
Methods
impl Process
fn kill(id: pid_t, signal: isize) -> IoResult<()>
Sends signal
to another process in the system identified by id
.
Note that windows doesn't quite have the same model as unix, so some
unix signals are mapped to windows signals. Notably, unix termination
signals (SIGTERM/SIGKILL/SIGINT) are translated to TerminateProcess
.
Additionally, a signal number of 0 can check for existence of the target process. Note, though, that on some platforms signals will continue to be successfully delivered if the child has exited, but not yet been reaped.
fn id(&self) -> pid_t
Returns the process id of this child process
fn signal(&mut self, signal: isize) -> IoResult<()>
Sends the specified signal to the child process, returning whether the signal could be delivered or not.
Note that signal 0 is interpreted as a poll to check whether the child process is still alive or not. If an error is returned, then the child process has exited.
On some unix platforms signals will continue to be received after a
child has exited but not yet been reaped. In order to report the status
of signal delivery correctly, unix implementations may invoke
waitpid()
with WNOHANG
in order to reap the child as necessary.
Errors
If the signal delivery fails, the corresponding error is returned.
fn signal_exit(&mut self) -> IoResult<()>
Sends a signal to this child requesting that it exits. This is equivalent to sending a SIGTERM on unix platforms.
fn signal_kill(&mut self) -> IoResult<()>
Sends a signal to this child forcing it to exit. This is equivalent to sending a SIGKILL on unix platforms.
fn wait(&mut self) -> IoResult<ProcessExit>
Wait for the child to exit completely, returning the status that it exited with. This function will continue to have the same return value after it has been called at least once.
The stdin handle to the child process will be closed before waiting.
Errors
This function can fail if a timeout was previously specified via
set_timeout
and the timeout expires before the child exits.
fn set_timeout(&mut self, timeout_ms: Option<u64>)
Sets a timeout, in milliseconds, for future calls to wait().
The argument specified is a relative distance into the future, in milliseconds, after which any call to wait() will return immediately with a timeout error, and all future calls to wait() will not block.
A value of None
will clear any previous timeout, and a value of Some
will override any previously set timeout.
Example
fn main() { use std::old_io::{Command, IoResult}; use std::old_io::process::ProcessExit; fn run_gracefully(prog: &str) -> IoResult<ProcessExit> { let mut p = try!(Command::new("long-running-process").spawn()); // give the process 10 seconds to finish completely p.set_timeout(Some(10_000)); match p.wait() { Ok(status) => return Ok(status), Err(..) => {} } // Attempt to exit gracefully, but don't wait for it too long try!(p.signal_exit()); p.set_timeout(Some(1_000)); match p.wait() { Ok(status) => return Ok(status), Err(..) => {} } // Well, we did our best, forcefully kill the process try!(p.signal_kill()); p.set_timeout(None); p.wait() } }use std::old_io::{Command, IoResult}; use std::old_io::process::ProcessExit; fn run_gracefully(prog: &str) -> IoResult<ProcessExit> { let mut p = try!(Command::new("long-running-process").spawn()); // give the process 10 seconds to finish completely p.set_timeout(Some(10_000)); match p.wait() { Ok(status) => return Ok(status), Err(..) => {} } // Attempt to exit gracefully, but don't wait for it too long try!(p.signal_exit()); p.set_timeout(Some(1_000)); match p.wait() { Ok(status) => return Ok(status), Err(..) => {} } // Well, we did our best, forcefully kill the process try!(p.signal_kill()); p.set_timeout(None); p.wait() }
fn wait_with_output(self) -> IoResult<ProcessOutput>
Simultaneously wait for the child to exit and collect all remaining
output on the stdout/stderr handles, returning a ProcessOutput
instance.
The stdin handle to the child is closed before waiting.
Errors
This function can fail for any of the same reasons that wait()
can
fail.
fn forget(self)
Forgets this process, allowing it to outlive the parent
This function will forcefully prevent calling wait()
on the child
process in the destructor, allowing the child to outlive the
parent. Note that this operation can easily lead to leaking the
resources of the child process, so care must be taken when
invoking this method.