NSTask
:
- Can run his task in the background. Allows you to send interrupts and kills to the underlying process, and allows you to suspend or resume the underlying process without setting up threads yourself. Can also run synchronously if that's what you want.
- Let's you work back and forth with Cocoa classes, like
NSString
s without having to do a buncha conversions. - Let's you set I/O streams for the underlying process that differ from the caller's.
- Better supported across all Apple platforms (like iOS) than
system(3)
-- I don't thinksystem
even works on iOS. - Requires Cocoa and Objective-C.
- Doesn't interpret shell arguments or do path expansions of arguments.
system(3)
:
- Better supported across all Unix-like platforms.
- Can run a task with a one-liner.
- Only requires C.
- Runs in a shell and will interpret working directory and arguments like
/bin/sh
would.
For a Cocoa app I always use NSTask
; I only use system
if I'm doing something that must be C-only or I know will have to run under non-Mac environments. As it is, system
is pretty brittle and the more robust solution is doing a fork
-exec
, because it allows you more control over streams and concurrent operation.