#process #tokio #future #management #command #status

nightly bin+lib tokio-process

An implementation of an asynchronous process management backed futures

14 releases

0.3.0-alpha.2 Aug 19, 2019
0.2.5 Feb 5, 2020
0.2.4 Jun 22, 2019
0.2.3 Nov 2, 2018
0.1.0 Sep 10, 2016

#245 in #asynchronous

Download history 1499/week @ 2024-02-26 1347/week @ 2024-03-04 1304/week @ 2024-03-11 1252/week @ 2024-03-18 1241/week @ 2024-03-25 1564/week @ 2024-04-01 1297/week @ 2024-04-08 1477/week @ 2024-04-15 1444/week @ 2024-04-22 1334/week @ 2024-04-29 1400/week @ 2024-05-06 1442/week @ 2024-05-13 1603/week @ 2024-05-20 1272/week @ 2024-05-27 1426/week @ 2024-06-03 1187/week @ 2024-06-10

5,605 downloads per month
Used in fewer than 17 crates

MIT license

10K SLoC


An implementation of process management for Tokio


This project is licensed under the MIT license.


Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in Tokio by you, shall be licensed as MIT, without any additional terms or conditions.


An implementation of asynchronous process management for Tokio.

This crate provides a Command struct that imitates the interface of the std::process::Command type in the standard library, but provides asynchronous versions of functions that create processes. These functions (spawn, status, output and their variants) return "future aware" types that interoperate with Tokio. The asynchronous process support is provided through signal handling on Unix and system APIs on Windows.


Here's an example program which will spawn echo hello world and then wait for it complete.


use tokio_process::Command;

async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // The usage is the same as with the standard library's `Command` type, however the value
    // returned from `spawn` is a `Result` containing a `Future`.
    let child = Command::new("echo").arg("hello").arg("world")

    // Make sure our child succeeded in spawning and process the result
    let future = child.expect("failed to spawn");

    // Await until the future (and the command) completes
    let status = future.await?;
    println!("the command exited with: {}", status);

Next, let's take a look at an example where we not only spawn echo hello world but we also capture its output.


use tokio_process::Command;

async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Like above, but use `output` which returns a future instead of
    // immediately returning the `Child`.
    let output = Command::new("echo").arg("hello").arg("world")

    let output = output.await?;

    assert_eq!(output.stdout, b"hello world\n");

We can also read input line by line.


use futures_util::stream::StreamExt;
use std::process::{Stdio};
use tokio::codec::{FramedRead, LinesCodec};
use tokio_process::Command;

async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut cmd = Command::new("cat");

    // Specify that we want the command's standard output piped back to us.
    // By default, standard input/output/error will be inherited from the
    // current process (for example, this means that standard input will
    // come from the keyboard and standard output/error will go directly to
    // the terminal if this process is invoked from the command line).

    let mut child = cmd.spawn()
        .expect("failed to spawn command");

    let stdout = child.stdout().take()
        .expect("child did not have a handle to stdout");

    let mut reader = FramedRead::new(stdout, LinesCodec::new());

    // Ensure the child process is spawned in the runtime so it can
    // make progress on its own while we await for any output.
    tokio::spawn(async {
        let status = child.await
            .expect("child process encountered an error");

        println!("child status was: {}", status);

    while let Some(line) = reader.next().await {
        println!("Line: {}", line?);



While similar to the standard library, this crate's Child type differs importantly in the behavior of drop. In the standard library, a child process will continue running after the instance of std::process::Child is dropped. In this crate, however, because tokio_process::Child is a future of the child's ExitStatus, a child process is terminated if tokio_process::Child is dropped. The behavior of the standard library can be regained with the Child::forget method.


~61K SLoC