Class

Process

An object representing a subprocess of the current process.

Overview

Using the Process class, your program can run another program as a subprocess and can monitor that program’s execution. A Process object creates a separate executable entity; it differs from Thread in that it does not share memory space with the process that creates it.

A process operates within an environment defined by the current values for several items: the current directory, standard input, standard output, standard error, and the values of any environment variables. By default, an Process object inherits its environment from the process that launches it. If there are any values that should be different for the process, for example, if the current directory should change, you must change the value before you launch it. A process's environment cannot be changed while it is running.

An Process object can only be run once. Subsequent attempts raise an error.

Topics

Creating and Initializing an NSTask Object

class func launchedProcess(launchPath: String, arguments: [String])

Creates and launches a task with a specified executable and arguments.

Deprecated
init()

Returns an initialized NSTask object with the environment of the current process.

Returning Task Information

var arguments: [String]?

Sets the command arguments that should be used to launch the executable.

var currentDirectoryPath: String

Sets the current directory for the receiver.

Deprecated
var environment: [String : String]?

Sets the environment for the receiver.

var launchPath: String?

Sets the receiver’s executable.

Deprecated
var processIdentifier: Int32

Returns the receiver’s process identifier.

var standardError: Any?

Sets the standard error for the receiver.

var standardInput: Any?

Sets the standard input for the receiver.

var standardOutput: Any?

Sets the standard output for the receiver.

Running and Stopping a Task

func interrupt()

Sends an interrupt signal to the receiver and all of its subtasks.

func launch()

Launches the task represented by the receiver.

Deprecated
func resume()

Resumes execution of the receiver task that had previously been suspended with a suspend() message.

func suspend()

Suspends execution of the receiver task.

func terminate()

Sends a terminate signal to the receiver and all of its subtasks.

func waitUntilExit()

Block until the receiver is finished.

Querying the Task State

var terminationStatus: Int32

Returns the exit status returned by the receiver’s executable.

var terminationReason: Process.TerminationReason

Returns the reason the task was terminated.

Configuring an NSTask Object

var arguments: [String]?

Sets the command arguments that should be used to launch the executable.

var currentDirectoryPath: String

Sets the current directory for the receiver.

Deprecated
var environment: [String : String]?

Sets the environment for the receiver.

var launchPath: String?

Sets the receiver’s executable.

Deprecated
var standardError: Any?

Sets the standard error for the receiver.

var standardInput: Any?

Sets the standard input for the receiver.

var standardOutput: Any?

Sets the standard output for the receiver.

Task Termination Handler

var terminationHandler: ((Process) -> Void)?

Invoked when the task is completed.

Constants

enum Process.TerminationReason

These constants specify the values that are returned by terminationReason.

enum QualityOfService

Used to indicate the nature and importance of work to the system. Work with higher quality of service classes receive more resources than work with lower quality of service classes whenever there is resource contention.

Notifications

class let didTerminateNotification: NSNotification.Name

Posted when the task has stopped execution. This notification can be posted either when the task has exited normally or as a result of terminate() being sent to the NSTask object. If the NSTask object gets released, however, this notification will not get sent, as the port the message would have been sent on was released as part of the task release. The observer method can use terminationStatus to determine why the task died. See Ending an NSTask for an example.

Instance Methods

Relationships

Inherits From

Conforms To

See Also

Tasks and Pipes

class Pipe

A one-way communications channel between related processes.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software