使用NSTask启动命令返回错误 [英] Launching command using NSTask returns error

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

问题描述

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

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

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

这是我要做的代码:
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:无此类文件或目录

/bin/sh: sudo: No such file or directory


推荐答案

我找到了解决方法:

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆