问题
I'd like to launch the following command from my application using NSTask:
sudo -u myusername launchctl load /Library/LaunchAgents/com.google.keystone.agent.plist
Here is a code I do: NSPipe *pipe = [NSPipe pipe];
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath: @"/bin/sh"];
[task setCurrentDirectoryPath:@"/"];
[task setStandardError:pipe];
NSArray *arguments = nil;
arguments = @[@"sudo",
@"-u",
@"myusername",
@"launchctl",
@"load",
@"/Library/LaunchAgents/com.google.keystone.agent.plist"];
[task setArguments: arguments];
NSFileHandle * read = [pipe fileHandleForReading];
[task launch];
[task waitUntilExit];
NSData * dataRead = [read readDataToEndOfFile];
NSString * output = [[NSString alloc] initWithData:dataRead encoding:NSUTF8StringEncoding];
NSLog(@"output: %@", output);
After processing I receive an error below:
/bin/sh: sudo: No such file or directory
回答1:
I've found out solution:
arguments = @[@"-c",
@"sudo -u myusername launchctl load /Library/LaunchAgents/com.google.keystone.agent.plist"];
来源:https://stackoverflow.com/questions/24137738/launching-command-using-nstask-returns-error