Process - noppoMan/Suv GitHub Wiki

Process class is for managing child process

equivalent to libuv's process

Spawn

Spawning child process

Basic

let ls = try! ChildProcess.spawn("ls", ["-la", "/my/path"])

ls.onExit {
  print(ls.status) // this is exit status code
}

ls.stdout?.read { result in
  if case let .Data(buf) = result {
      print(buf.toString())
  } else if case .Error = result {
      print("error")
  } else if case .EOF = result {
    print("EOF")
  }
}

ls.stderr?.read { result in
  if case let .Data(buf) = result {
      print(buf.toString())
  } else if case .Error = result {
      print("error")
  } else if case .EOF = result {
    print("EOF")
  }
}

detach

if options.detached is set to true, the child process will be made the leader of a new process group and session. Note that child processes may continue running after the parent exits regardless of whether they are detached or not.

var ops = SpawnOptions()
opts.detached = true

let ls = try! ChildProcess.spawn("ls", ["-la", "/my/path"], options: ops)

stdio

The options.stdio option is used to configure the pipes that are established between the parent and child process. By default, the child's stdin, stdout, and stderr are redirected to corresponding child.stdin, child.stdout, and child.stderr streams on the ChildProcess object. This is equivalent to setting the options.stdio equal to

let stdio = [
    // stdin
    StdioOption(flags: .CreateReadablePipe, pipe: Pipe(loop: loop)),

    // stdout
    StdioOption(flags: .CreateWritablePipe, pipe: Pipe(loop: loop)),

    // stderr
    StdioOption(flags: .CreateWritablePipe, pipe: Pipe(loop: loop))
]

pipe

Readable Pipe from client.

StdioOption(flags: .CreateReadablePipe, pipe: Pipe(loop: loop))

Writable Pipe from client.

StdioOption(flags: .CreateWritablePipe, pipe: Pipe(loop: loop))

ignore

StdioOption(flags: .Ignore)

InheritFd

StdioOption(flags: .InheritFd, fd: 0)

InheritStream

StdioOption(flags: .InheritStream, pipe: Pipe(loop: loop))