spawnProcess (Function)

Signature: spawnProcess(command: string, callback: (int, int, string) -> Unit) -> Result<int, Error>

Description: Spawns a process and reports stdout, stderr and exit events through the required callback. Returns a handle for the running process.

Parameters

  • command (string): The command to execute
  • callback ((int, int, string) -> Unit): Required callback receiving (processID, eventType, data); event types are stdout (1), stderr (2) and exit (3)

Returns: Result<int, Error>

Example

fn onEvent(_pid, kind, data) = print("${kind}: ${trim(data)}")
match spawnProcess("echo hello", onEvent) {
    Success { value } => {
        let exitCode = awaitProcess(value)
        cleanupProcess(value)
        print("exit: ${exitCode}")
    }
    Error { message } => print("Process failed: ${message}")
}
onEvent (_pid, kind, data) = print "${kind}: ${trim data}"
match spawnProcess "echo hello" onEvent
    Success value =>
        exitCode = awaitProcess value
        cleanupProcess value
        print "exit: ${exitCode}"
    Error message => print "Process failed: ${message}"