# Process

<a id="process-3"></a>

# Process

```cpp
#include <icy/process.h>

class Process
```

[Defined in src/base/include/icy/process.h:32](https://github.com/auscaster/icey/blob/main/src/base/include/icy/process.h#L32)

Spawns and manages a child process with stdin/stdout/stderr pipes.

## Public Attributes

| Return | Name | Description |
|--------|------|-------------|
| `std::string` | [`file`](#file)  | Path to the program to execute. Convenience proxy for options.file. Must be set before [`spawn()`](#spawn) |
| `std::string` | [`cwd`](#cwd)  | Set the current working directory. Convenience proxy for options.cwd. Must be set before [`spawn()`](#spawn) |
| `std::vector< std::string >` | [`args`](#args-1)  | Command line arguments to pass to the process. Convenience proxy for options.args. Must be set before [`spawn()`](#spawn) |
| `std::vector< std::string >` | [`env`](#env)  | Environment variables for the process. Each entry should be in "KEY=VALUE" format. If empty, the child inherits the parent environment. Must be set before [`spawn()`](#spawn) |
| `std::function< void(std::string)>` | [`onstdout`](#onstdout)  | Stdout callback. Called when a line has been output from the process. |
| `std::function< void(std::string)>` | [`onstderr`](#onstderr)  | Stderr callback. Called when a line has been output on stderr. |
| `std::function< void(std::int64_t)>` | [`onexit`](#onexit)  | Exit callback. Called with process exit status code. |
| [`ProcessOptions`](https://auscaster.com/icey/docs/api-reference/base.md#processoptions) | [`options`](#options)  | LibUV C options. Available for advanced use cases. |

---

<a id="file"></a>

### file

```cpp
std::string file
```

[Defined in src/base/include/icy/process.h:55](https://github.com/auscaster/icey/blob/main/src/base/include/icy/process.h#L55)

Path to the program to execute. Convenience proxy for options.file. Must be set before [`spawn()`](#spawn)

---

<a id="cwd"></a>

### cwd

```cpp
std::string cwd
```

[Defined in src/base/include/icy/process.h:60](https://github.com/auscaster/icey/blob/main/src/base/include/icy/process.h#L60)

Set the current working directory. Convenience proxy for options.cwd. Must be set before [`spawn()`](#spawn)

---

<a id="args-1"></a>

### args

```cpp
std::vector< std::string > args
```

[Defined in src/base/include/icy/process.h:65](https://github.com/auscaster/icey/blob/main/src/base/include/icy/process.h#L65)

Command line arguments to pass to the process. Convenience proxy for options.args. Must be set before [`spawn()`](#spawn)

---

<a id="env"></a>

### env

```cpp
std::vector< std::string > env
```

[Defined in src/base/include/icy/process.h:71](https://github.com/auscaster/icey/blob/main/src/base/include/icy/process.h#L71)

Environment variables for the process. Each entry should be in "KEY=VALUE" format. If empty, the child inherits the parent environment. Must be set before [`spawn()`](#spawn)

---

<a id="onstdout"></a>

### onstdout

```cpp
std::function< void(std::string)> onstdout
```

[Defined in src/base/include/icy/process.h:97](https://github.com/auscaster/icey/blob/main/src/base/include/icy/process.h#L97)

Stdout callback. Called when a line has been output from the process.

---

<a id="onstderr"></a>

### onstderr

```cpp
std::function< void(std::string)> onstderr
```

[Defined in src/base/include/icy/process.h:101](https://github.com/auscaster/icey/blob/main/src/base/include/icy/process.h#L101)

Stderr callback. Called when a line has been output on stderr.

---

<a id="onexit"></a>

### onexit

```cpp
std::function< void(std::int64_t)> onexit
```

[Defined in src/base/include/icy/process.h:105](https://github.com/auscaster/icey/blob/main/src/base/include/icy/process.h#L105)

Exit callback. Called with process exit status code.

---

<a id="options"></a>

### options

```cpp
ProcessOptions options
```

Type: [`ProcessOptions`](https://auscaster.com/icey/docs/api-reference/base.md#processoptions)

[Defined in src/base/include/icy/process.h:109](https://github.com/auscaster/icey/blob/main/src/base/include/icy/process.h#L109)

LibUV C options. Available for advanced use cases.

## Public Methods

| Return | Name | Description |
|--------|------|-------------|
|  | [`Process`](#process-4)  | Constructs a [`Process`](#process-3) attached to the given event loop. |
|  | [`Process`](#process-5)  | Constructs a [`Process`](#process-3) with initial command-line arguments. |
|  | [`~Process`](#process-6)  | Destructor. |
|  | [`Process`](#process-7)  | Deleted constructor. |
| [`Process`](#process-3) & | [`operator=`](#operator-76)  | Deleted assignment operator. |
|  | [`Process`](#process-8)  | Deleted constructor. |
| [`Process`](#process-3) & | [`operator=`](#operator-77)  | Deleted assignment operator. |
| `void` | [`spawn`](#spawn)  | Spawns the process. Options must be properly set. Throws an exception on error. |
| `bool` | [`kill`](#kill)  | Sends a signal to the process. |
| `int` | [`pid`](#pid) `const` | Returns the process PID, or 0 if not spawned. |
| [`Pipe`](https://auscaster.com/icey/docs/api-reference/icy-Pipe.md#pipe) & | [`in`](#in)  | Returns the stdin pipe. |
| [`Pipe`](https://auscaster.com/icey/docs/api-reference/icy-Pipe.md#pipe) & | [`out`](#out)  | Returns the stdout pipe. |
| [`Pipe`](https://auscaster.com/icey/docs/api-reference/icy-Pipe.md#pipe) & | [`err`](#err-1)  | Returns the stderr pipe. |

---

<a id="process-4"></a>

### Process

```cpp
Process(uv::Loop * loop = uv::defaultLoop())
```

[Defined in src/base/include/icy/process.h:37](https://github.com/auscaster/icey/blob/main/src/base/include/icy/process.h#L37)

Constructs a [`Process`](#process-3) attached to the given event loop.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `loop` | [`uv::Loop`](https://auscaster.com/icey/docs/api-reference/uv.md#loop-1) * | Event loop to use for I/O and exit notifications. Defaults to the default loop. |

---

<a id="process-5"></a>

### Process

```cpp
Process(std::initializer_list< std::string > args, uv::Loop * loop = uv::defaultLoop())
```

[Defined in src/base/include/icy/process.h:42](https://github.com/auscaster/icey/blob/main/src/base/include/icy/process.h#L42)

Constructs a [`Process`](#process-3) with initial command-line arguments.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `args` | `std::initializer_list< std::string >` | Initializer list of argument strings. The first element is typically the executable path. |
| `loop` | [`uv::Loop`](https://auscaster.com/icey/docs/api-reference/uv.md#loop-1) * | Event loop to use for I/O and exit notifications. Defaults to the default loop. |

---

<a id="process-6"></a>

### ~Process

```cpp
~Process()
```

[Defined in src/base/include/icy/process.h:45](https://github.com/auscaster/icey/blob/main/src/base/include/icy/process.h#L45)

Destructor.

---

<a id="process-7"></a>

### Process

```cpp
Process(const Process &) = delete
```

[Defined in src/base/include/icy/process.h:47](https://github.com/auscaster/icey/blob/main/src/base/include/icy/process.h#L47)

Deleted constructor.

---

<a id="operator-76"></a>

### operator=

```cpp
Process & operator=(const Process &) = delete
```

[Defined in src/base/include/icy/process.h:48](https://github.com/auscaster/icey/blob/main/src/base/include/icy/process.h#L48)

Deleted assignment operator.

---

<a id="process-8"></a>

### Process

```cpp
Process(Process &&) = delete
```

[Defined in src/base/include/icy/process.h:49](https://github.com/auscaster/icey/blob/main/src/base/include/icy/process.h#L49)

Deleted constructor.

---

<a id="operator-77"></a>

### operator=

```cpp
Process & operator=(Process &&) = delete
```

[Defined in src/base/include/icy/process.h:50](https://github.com/auscaster/icey/blob/main/src/base/include/icy/process.h#L50)

Deleted assignment operator.

---

<a id="spawn"></a>

### spawn

```cpp
void spawn()
```

[Defined in src/base/include/icy/process.h:76](https://github.com/auscaster/icey/blob/main/src/base/include/icy/process.h#L76)

Spawns the process. Options must be properly set. Throws an exception on error.

---

<a id="kill"></a>

### kill

```cpp
bool kill(int signum = SIGTERM)
```

[Defined in src/base/include/icy/process.h:81](https://github.com/auscaster/icey/blob/main/src/base/include/icy/process.h#L81)

Sends a signal to the process. 
#### Returns
True if the signal was sent successfully, false if the process is not running or handle is invalid.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `signum` | `int` | [Signal](https://auscaster.com/icey/docs/api-reference/icy-Signal.md#signal) number to send (default: `SIGTERM`). |

---

<a id="pid"></a>

### pid

`const`

```cpp
int pid() const
```

[Defined in src/base/include/icy/process.h:84](https://github.com/auscaster/icey/blob/main/src/base/include/icy/process.h#L84)

Returns the process PID, or 0 if not spawned.

---

<a id="in"></a>

### in

```cpp
Pipe & in()
```

[Defined in src/base/include/icy/process.h:87](https://github.com/auscaster/icey/blob/main/src/base/include/icy/process.h#L87)

Returns the stdin pipe.

---

<a id="out"></a>

### out

```cpp
Pipe & out()
```

[Defined in src/base/include/icy/process.h:90](https://github.com/auscaster/icey/blob/main/src/base/include/icy/process.h#L90)

Returns the stdout pipe.

---

<a id="err-1"></a>

### err

```cpp
Pipe & err()
```

[Defined in src/base/include/icy/process.h:93](https://github.com/auscaster/icey/blob/main/src/base/include/icy/process.h#L93)

Returns the stderr pipe.

## Protected Attributes

| Return | Name | Description |
|--------|------|-------------|
| [`uv::Handle`](https://auscaster.com/icey/docs/api-reference/icy-uv-Handle.md#handle-6)< uv_process_t > | [`_handle`](#_handle-1)  |  |
| [`Pipe`](https://auscaster.com/icey/docs/api-reference/icy-Pipe.md#pipe) | [`_stdin`](#_stdin)  |  |
| [`Pipe`](https://auscaster.com/icey/docs/api-reference/icy-Pipe.md#pipe) | [`_stdout`](#_stdout)  |  |
| [`Pipe`](https://auscaster.com/icey/docs/api-reference/icy-Pipe.md#pipe) | [`_stderr`](#_stderr)  |  |
| `uv_stdio_container_t` | [`_stdio`](#_stdio)  |  |
| `std::vector< char * >` | [`_cargs`](#_cargs)  |  |
| `std::vector< char * >` | [`_cenv`](#_cenv)  |  |

---

<a id="_handle-1"></a>

### _handle

```cpp
uv::Handle< uv_process_t > _handle
```

Type: [`uv::Handle`](https://auscaster.com/icey/docs/api-reference/icy-uv-Handle.md#handle-6)< uv_process_t >

[Defined in src/base/include/icy/process.h:114](https://github.com/auscaster/icey/blob/main/src/base/include/icy/process.h#L114)

---

<a id="_stdin"></a>

### _stdin

```cpp
Pipe _stdin
```

Type: [`Pipe`](https://auscaster.com/icey/docs/api-reference/icy-Pipe.md#pipe)

[Defined in src/base/include/icy/process.h:115](https://github.com/auscaster/icey/blob/main/src/base/include/icy/process.h#L115)

---

<a id="_stdout"></a>

### _stdout

```cpp
Pipe _stdout
```

Type: [`Pipe`](https://auscaster.com/icey/docs/api-reference/icy-Pipe.md#pipe)

[Defined in src/base/include/icy/process.h:116](https://github.com/auscaster/icey/blob/main/src/base/include/icy/process.h#L116)

---

<a id="_stderr"></a>

### _stderr

```cpp
Pipe _stderr
```

Type: [`Pipe`](https://auscaster.com/icey/docs/api-reference/icy-Pipe.md#pipe)

[Defined in src/base/include/icy/process.h:117](https://github.com/auscaster/icey/blob/main/src/base/include/icy/process.h#L117)

---

<a id="_stdio"></a>

### _stdio

```cpp
uv_stdio_container_t _stdio[3]
```

[Defined in src/base/include/icy/process.h:118](https://github.com/auscaster/icey/blob/main/src/base/include/icy/process.h#L118)

---

<a id="_cargs"></a>

### _cargs

```cpp
std::vector< char * > _cargs
```

[Defined in src/base/include/icy/process.h:119](https://github.com/auscaster/icey/blob/main/src/base/include/icy/process.h#L119)

---

<a id="_cenv"></a>

### _cenv

```cpp
std::vector< char * > _cenv
```

[Defined in src/base/include/icy/process.h:120](https://github.com/auscaster/icey/blob/main/src/base/include/icy/process.h#L120)

## Protected Methods

| Return | Name | Description |
|--------|------|-------------|
| `void` | [`init`](#init-4)  |  |

---

<a id="init-4"></a>

### init

```cpp
void init()
```

[Defined in src/base/include/icy/process.h:112](https://github.com/auscaster/icey/blob/main/src/base/include/icy/process.h#L112)
