Using the NSTask
class, your program can run another program as a subprocess and can monitor that program’s execution. An NSTask
object creates a separate executable entity; it differs from NSThread
in that it does not share memory space with the process that creates it.
A task 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, anNSTask
object inherits its environment from the process that launches it. If there are any values that should be different for the task, for example, if the current directory should change, you must change the value before you launch the task. A task’s environment cannot be changed while it is running.
An NSTask
object can only be run once. Subsequent attempts to run the task raise an error.
Important: In a sandboxed application, child processes created with the NSTask
class inherit the sandbox of the parent app. You should generally write helper applications as XPC Services instead, because XPC Services allows you to specify different sandbox entitlements for helper apps. See Daemons and Services Programming Guide and XPC Services API Reference for more information.