2

我想使用 NSTask 从我的应用程序中启动以下命令:

sudo -u myusername launchctl load /Library/LaunchAgents/com.google.keystone.agent.plist

这是我做的代码: 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);

处理后,我收到以下错误:

/bin/sh: sudo: 没有这样的文件或目录

4

1 回答 1

1

我找到了解决方案:

arguments = @[@"-c",
              @"sudo -u myusername launchctl load /Library/LaunchAgents/com.google.keystone.agent.plist"];
于 2014-06-10T10:14:35.967 回答