Inherits from | |
Conforms to | |
Framework | /System/Library/Frameworks/Foundation.framework |
Availability | Available in Mac OS X v10.0 and later. |
Companion guide | |
Declared in | NSTask.h |
Related sample code |
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, an NSTask
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.
– arguments
– currentDirectoryPath
– environment
– launchPath
– processIdentifier
– standardError
– standardInput
– standardOutput
– setArguments:
– setCurrentDirectoryPath:
– setEnvironment:
– setLaunchPath:
– setStandardError:
– setStandardInput:
– setStandardOutput:
Creates and launches a task with a specified executable and arguments.
+ (NSTask *)launchedTaskWithLaunchPath:(NSString *)path arguments:(NSArray *)arguments
The path to the executable.
An array of NSString
objects that supplies the arguments to the task. If arguments is nil
, an NSInvalidArgumentException
is raised.
The task inherits its environment from the process that invokes this method.
The NSTask
object converts both path and the strings in arguments to appropriate C-style strings (using fileSystemRepresentation
) before passing them to the task via argv[])
. The strings in arguments do not undergo shell expansion, so you do not need to do special quoting, and shell variables, such as $PWD
, are not resolved.
NSTask.h
Returns the arguments used when the receiver was launched.
- (NSArray *)arguments
An array of NSString
objects containing the arguments used when the receiver was launched.
NSTask.h
Returns the task’s current directory.
- (NSString *)currentDirectoryPath
The task's current working directory.
NSTask.h
Returns a dictionary of variables for the environment from which the receiver was launched.
- (NSDictionary *)environment
A dictionary of variables for the environment from which the receiver was launched. The dictionary keys are the environment variable names.
– setEnvironment:
– environment
(NSProcessInfo)NSTask.h
Returns an initialized NSTask
object with the environment of the current process.
- (id)init
An initialized NSTask
object with the environment of the current process.
If you need to modify the environment of a task, use alloc and init, and then set up the environment before launching the new task. Otherwise, just use the class method launchedTaskWithLaunchPath:arguments:
to create and run the task.
NSTask.h
Sends an interrupt signal to the receiver and all of its subtasks.
- (void)interrupt
If the task terminates as a result, which is the default behavior, an NSTaskDidTerminateNotification
gets sent to the default notification center. This method has no effect if the receiver was already launched and has already finished executing. If the receiver has not been launched yet, this method raises an NSInvalidArgumentException
.
It is not always possible to interrupt the receiver because it might be ignoring the interrupt signal. interrupt
sends SIGINT
.
NSTask.h
Returns whether the receiver is still running.
- (BOOL)isRunning
YES
if the receiver is still running, otherwise NO
. NO
means either the receiver could not run or it has terminated.
NSTask.h
Launches the task represented by the receiver.
- (void)launch
Raises an NSInvalidArgumentException
if the launch path has not been set or is invalid or if it fails to create a process.
NSTask.h
Returns the path of the receiver’s executable.
- (NSString *)launchPath
The path of the receiver’s executable.
NSTask.h
Returns the receiver’s process identifier.
- (int)processIdentifier
The receiver’s process identifier.
NSTask.h
Resumes execution of the receiver task that had previously been suspended with a suspend
message.
- (BOOL)resume
YES
if the receiver was able to resume execution, NO
otherwise.
If multiple suspend
messages were sent to the receiver, an equal number of resume
messages must be sent before the task resumes execution.
NSTask.h
Sets the command arguments that should be used to launch the executable.
- (void)setArguments:(NSArray *)arguments
An array of NSString
objects that supplies the arguments to the task. If arguments is nil
, an NSInvalidArgumentException
is raised.
The NSTask
object converts both path and the strings in arguments to appropriate C-style strings (using fileSystemRepresentation
) before passing them to the task via argv[]
. The strings in arguments do not undergo shell expansion, so you do not need to do special quoting, and shell variables, such as $PWD
, are not resolved.
NSTask.h
Sets the current directory for the receiver.
- (void)setCurrentDirectoryPath:(NSString *)path
The current directory for the task.
If this method isn’t used, the current directory is inherited from the process that created the receiver. This method raises an NSInvalidArgumentException
if the receiver has already been launched.
NSTask.h
Sets the environment for the receiver.
- (void)setEnvironment:(NSDictionary *)environmentDictionary
A dictionary of environment variable values whose keys are the variable names.
If this method isn’t used, the environment is inherited from the process that created the receiver. This method raises an NSInvalidArgumentException
if the receiver has already been launched.
NSTask.h
Sets the receiver’s executable.
- (void)setLaunchPath:(NSString *)path
The path to the executable.
NSTask.h
Sets the standard error for the receiver.
- (void)setStandardError:(id)file
The standard error for the receiver, which can be either an NSFileHandle
or an NSPipe
object.
If file is an NSPipe
object, launching the receiver automatically closes the write end of the pipe in the current task. Don’t create a handle for the pipe and pass that as the argument, or the write end of the pipe won’t be closed automatically.
If this method isn’t used, the standard error is inherited from the process that created the receiver. This method raises an NSInvalidArgumentException
if the receiver has already been launched.
NSTask.h
Sets the standard input for the receiver.
- (void)setStandardInput:(id)file
The standard input for the receiver, which can be either an NSFileHandle
or an NSPipe
object.
If file is an NSPipe
object, launching the receiver automatically closes the read end of the pipe in the current task. Don’t create a handle for the pipe and pass that as the argument, or the read end of the pipe won’t be closed automatically.
If this method isn’t used, the standard input is inherited from the process that created the receiver. This method raises an NSInvalidArgumentException
if the receiver has already been launched.
NSTask.h
Sets the standard output for the receiver.
- (void)setStandardOutput:(id)file
The standard output for the receiver, which can be either an NSFileHandle
or an NSPipe
object.
If file is an NSPipe
object, launching the receiver automatically closes the write end of the pipe in the current task. Don’t create a handle for the pipe and pass that as the argument, or the write end of the pipe won’t be closed automatically.
If this method isn’t used, the standard output is inherited from the process that created the receiver. This method raises an NSInvalidArgumentException
if the receiver has already been launched.
NSTask.h
Returns the standard error file used by the receiver.
- (id)standardError
The standard error file used by the receiver.
Standard error is where all diagnostic messages are sent. The object returned is either an NSFileHandle
or an NSPipe
instance, depending on what type of object was passed to setStandardError:
.
NSTask.h
Returns the standard input file used by the receiver.
- (id)standardInput
The standard input file used by the receiver.
Standard input is where the receiver takes its input from unless otherwise specified. The object returned is either an NSFileHandle
or an NSPipe
instance, depending on what type of object was passed to the setStandardInput:
method.
NSTask.h
Returns the standard output file used by the receiver.
- (id)standardOutput
The standard output file used by the receiver.
Standard output is where the receiver displays its output. The object returned is either an NSFileHandle
or an NSPipe
instance, depending on what type of object was passed to the setStandardOutput:
method.
NSTask.h
Suspends execution of the receiver task.
- (BOOL)suspend
YES
if the receiver was successfully suspended, NO
otherwise.
Multiple suspend
messages can be sent, but they must be balanced with an equal number of resume
messages before the task resumes execution.
NSTask.h
Sends a terminate signal to the receiver and all of its subtasks.
- (void)terminate
If the task terminates as a result, which is the default behavior, an NSTaskDidTerminateNotification
gets sent to the default notification center. This method has no effect if the receiver was already launched and has already finished executing. If the receiver has not been launched yet, this method raises an NSInvalidArgumentException
.
It is not always possible to terminate the receiver because it might be ignoring the terminate signal. terminate
sends SIGTERM
.
NSTask.h
Returns the exit status returned by the receiver’s executable.
- (int)terminationStatus
The exit status returned by the receiver’s executable.
Each task defines and documents how its return value should be interpreted. For example, many commands return 0 if they complete successfully or an error code if they don’t. You’ll need to look at the documentation for that task to learn what values it returns under what circumstances.
This method raises an NSInvalidArgumentException
if the receiver is still running. Verify that the receiver is not running before you use it.
if (![aTask isRunning]) { |
int status = [aTask terminationStatus]; |
if (status == ATASK_SUCCESS_VALUE) |
NSLog(@"Task succeeded."); |
else |
NSLog(@"Task failed."); |
} |
NSTask.h
Block until the receiver is finished.
- (void)waitUntilExit
This method first checks to see if the receiver is still running using isRunning
. Then it polls the current run loop using NSDefaultRunLoopMode
until the task completes.
[aTask launch]; |
[aTask waitUntilExit]; |
int status = [aTask terminationStatus]; |
if (status == ATASK_SUCCESS_VALUE) |
NSLog(@"Task succeeded."); |
else |
NSLog(@"Task failed."); |
NSTask.h
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.
The notification object is the NSTask
object that was terminated. This notification does not contain a userInfo dictionary.
NSTask.h
© 2007 Apple Inc. All Rights Reserved. (Last updated: 2007-01-31)