I want to start a command line program (deamon) from my Cocoa app. It
seems there are a lot of ways to do this. I found at least four:
- (void)startDeamon
{
NSString *execPath = @"path/to/executable";
NSString *execName = [execPath lastPathComponent];
// (1) LaunchServices
NSURL *deamonURL = [NSURL fileURLWithPath:execPath];
LSOpenCFURLRef((CFURLRef)deamonURL, NULL);
// (2) use system()
NSString *command = [execPath stringByAppendingString:@"&"];
system([command fileSystemRepresentation]);
// (3) the unix solution
if ( fork() == 0 ) {
// in child
const char *path = [execPath fileSystemRepresentation];
const char *name = [execName fileSystemRepresentation];
execl(path, name, NULL);
}
// (4) NSTask one liner
[NSTask launchedTaskWithLaunchPath:execPath arguments:[NSArray
array]];
// not sure about 'arguments' here...
}
Can anyone shed some light on the relative merits of these alternatives?
Are there any side effects I should be aware of?
Patrick
---
Hieper Software
w:
www.hieper.nl
e: info RemoveThis @hieper.nl