2

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

Serhii
  • 1,742
  • 2
  • 25
  • 54

1 Answers1

1

I've found out solution:

arguments = @[@"-c",
              @"sudo -u myusername launchctl load /Library/LaunchAgents/com.google.keystone.agent.plist"];
Serhii
  • 1,742
  • 2
  • 25
  • 54