私のアプリケーションはルートの下で起動しており、NSTask と launchctl を使用してプロセスをアンロードできるようにする必要があります。私が行うコードは次のとおりです。
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];
アンロードが必要なプロセスが「ルート」の下で起動された場合、アンロードは成功しますが、失敗しません。問題は、特定のユーザー (例: "myusername") で "launchctl" を実行する方法です。
編集:ターミナルで、特定のユーザーの下でコマンドを実行したい場合は、次に実行しますが、うまく機能します:
su - myusername -c "ls /Users/myusername"
しかし、特定のユーザーで「launchctl」を実行しようとすると失敗します。
su - myusername -c "launchctl load /Library/LaunchAgents/com.google.keystone.agent.plist"
「ロードするものが見つかりません」と表示されます