---
title: "Spawning a process in WASIX"
description: "This is a sample project that shows how to spawn a new process in WASIX."
url: "https://wasix.org/docs/language-guide/rust/tutorials/wasix-spawn/"
markdown: "https://wasix.org/docs/language-guide/rust/tutorials/wasix-spawn.md"
---

# Spawning a process in WASIX

This is a sample project that shows how to spawn a new `process` in WASIX.

## Prerequisites

> ⚠️
>
> Please check that you have the latest version of wasmer runtime as this tutorial depends on version **4.1.1** or higher.

The project requires the following to be installed on your system:

- [Rust ](https://www.rust-lang.org/tools/install)
- [WASIX](https://wasix.org/docs/language-guide/rust/installation.md)
- [Wasmer ](https://docs.wasmer.io/install)

## Start a new project

```shell
$ cargo new --bin wasix-spawn
     Created binary (application) `wasix-spawn` package
```

Your `wasix-spawn` directory structure should look like this:

- wasix-spawn

  - src
    - main.rs
  - .gitignore
  - Cargo.toml

## Writing the Application

Let’s write the application in `src/main.rs`:

```rust
use std::process::Command;

fn main() {
    std::env::set_var("PATH", "/bin"); // 1.

    let child = Command::new("ls") // 2.
        .arg("-l")
        .spawn()
        .expect("Failed to spawn child process");

    let result = child.wait_with_output().expect("Failed to wait on child"); // 3.

    println!("status: {}", result.status); // 4.
    println!("Child stdout: {}", String::from_utf8_lossy(&result.stdout)); // 4.
    println!("Child stderr: {}", String::from_utf8_lossy(&result.stderr)); // 4.
}
```

Let’s go through the code above:

1. We set the `PATH` environment variable to `/bin`. This is required because the `ls` command is located in `/bin` directory.
2. We spawn a new process using `Command::new("ls").arg("-l").spawn()`.
3. We wait for the child process to finish using `child.wait_with_output()`.
4. We print the status code, stdout and stderr of the child process.

> ℹ️
>
> The command is located in the `/bin` directory because our application would depend on `sharrattj/coreutils` which provides all the core utilities like `ls`, `cat`, `echo` etc. and places them in the `/bin` directory.

## Setting up our `wasmer.toml`

In this application, we need to setup our `wasmer.toml` file in order to use the `sharrattj/coreutils` package as a dependency. This is required because if we compile the program directly to WASIX, it would not work as there would be no binaries available.

Let’s setup our `wasmer.toml` file:

```toml
[package]
name = 'wasmer/wasix-spawn-example' # Replace with <your-namespace>/<your-package-name
version = '0.1.0'
description = 'An example of using spawn in wasix'
readme = 'README.md'

# See more keys and definitions at https://docs.wasmer.io/registry/manifest

[[module]]
name = 'wasix-spawn'
source = 'target/wasm32-wasmer-wasi/release/wasix-spawn.wasm'
abi = 'wasi'

[dependencies]
"sharrattj/coreutils" = "1"


[[command]]
name = 'wasix-spawn'
module = 'wasix-spawn'
```

## Running the Application

Let’s compile the application to WASIX and run it:

### Compiling to WASIX

```shell
$ cargo wasix build -r
   Compiling wasix-spawn v0.1.0 (.../wasix-rust-examples/wasix-spawn)
    Finished dev [unoptimized + debuginfo] target(s) in 0.37s
info: Post-processing WebAssembly files
  Optimizing with wasm-opt
```

Yay, it builds! Now, let’s try to run it:

### Running the Application with Wasmer

Now that we have compiled our application to WASIX, we can run it using Wasmer:

```shell
$ wasmer run target/wasm32-wasmer-wasi/release/wasix-spawn.wasm
---------- 1 somebody somegroup 0 Jan  1  1970 bin
---------- 1 somebody somegroup 0 Jan  1  1970 dev
---------- 1 somebody somegroup 0 Jan  1  1970 etc
---------- 1 somebody somegroup 0 Jan  1  1970 tmp
status: exit code: 0
Child stdout:
Child stderr:
```

You could also run the application using `wasmer run`:

```shell
$ wasmer run .
---------- 1 somebody somegroup 0 Jan  1  1970 bin
---------- 1 somebody somegroup 0 Jan  1  1970 dev
---------- 1 somebody somegroup 0 Jan  1  1970 etc
---------- 1 somebody somegroup 0 Jan  1  1970 tmp
status: exit code: 0
Child stdout:
Child stderr:
```

The above works because we have setup our `wasmer.toml` file with the `[[command]]` section.

Yay, it works! 🎉

# Exercises

## Exercise 1

Try to run the application with a different command.

## Exercise 2

Try to take the command as an argument from the user.

```shell
$ wasmer run . -- --command="ls"
```

> 💡
>
> You can use the `--` to pass arguments to the `.wasm` file and use the rust’s default `std::env::args` to parse the arguments. Learn more about [wasmer-cli ](https://docs.wasmer.io/runtime/cli).

# Conclusion

In this tutorial, we learned:

- How to spawn a new process in WASIX

[wasix-rust-examples/wasix-spawn](https://github.com/wasix-org/wasix-rust-examples/tree/main/wasix-spawn)

---

[Documentation index](https://wasix.org/llms.txt) · [HTML version](https://wasix.org/docs/language-guide/rust/tutorials/wasix-spawn/)
