My application runs as root, and I need to be able to offload processes using NSTask and launchctl. Here is the code I'm doing:
NSPipe *pipe = [NSPipe pipe];
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath: @"/bin/launchctl"];
[task setCurrentDirectoryPath:@"/"];
[task setStandardError:pipe];
NSLog(@"/bin/launchctl unload %@", plistAutostartLocation);
NSArray *arguments;
arguments = [NSArray arrayWithObjects: enableCommand, plistAutostartLocation, nil];
[task setArguments: arguments];
NSFileHandle * read = [pipe fileHandleForReading];
[task launch];
[task waitUntilExit];
If the process needs to be unloaded, it starts as "root", then it is unloaded successfully, if not a failure. The question is how to launch "launchctl" as a specific user (for example, "myusername")?
Edit: In the terminal, if I want to run some command under a specific user, I do the following and it works well:
su - myusername -c "ls / Users / myusername"
But when I try to run "launchctl" under a specific user, it fails:
su - myusername -c "launchctl load/Library/LaunchAgents/com.google.keystone.agent.plist"
: " "