本文介绍了使用NSTask启动命令返回错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'd like to launch the following command from my application using NSTask:

这是我要做的代码:
NSPipe *管道= [NSPipe pipe];

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:无此类文件或目录


推荐答案

我找到了解决方法:

arguments = @[@"-c",
              @"sudo -u myusername launchctl load /Library/LaunchAgents/com.google.keystone.agent.plist"];

这篇关于使用NSTask启动命令返回错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 13:17