我需要能够从根级别守护程序启动/停止每会话GUI代理。
我希望能够做的基本上是
for num in `ps ax | grep [s]bin/launchd | cut -c 1-5`;
do
if [ $num -ne 1 ];
then
sudo launchctl bsexec $num launchctl (un)load -S Aqua /Library/LaunchAgents/com.mycompany.mydaemon.plist;
fi;
done
但这仅启动/停止一个实例,并在当前GUI会话中以root身份运行。如果我离开sudo,那我就开始了
task_for_pid() (os/kern) failure
Couldn't switch to new bootstrap port: (ipc/send) invalid port right
我已经尝试过使用bsexec的各种其他排列(包括使用load / unload命令从bsexec调用辅助脚本),但我永远不会让实例以root身份启动,而不会在另一个GUI会话中启动。
我也试过搞乱 su - <user> ...
和 sudo -u <user> ...
,但也没有运气(正如许多人在上面的链接文章和其他地方讨论的那样)。
有没有人有任何想法?
编辑: 我尝试使用Graham Lee下面建议的包装工具执行此操作,但是我收到以下错误:
launch_msg(): Socket is not connected
这是我正在使用的命令行命令,包装器和脚本(501是用户ID,63093是另一个登录到系统的用户的launchd的pid):
命令行:
sudo launchctl bsexec 63093 /path/TestSetUIDAndExecuteTool 501 /path/LoadBillingDialogAgent
包装:
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
if (argc != 3) {
NSLog(@"Tool called with improper arguments");
return -1;
}
int uid = [[NSString stringWithUTF8String:argv[1]] intValue];
// TODO: REMOVE
NSLog(@"Setting uid to |%i|", uid);
setuid(uid);
// TODO: REMOVE
char *command = (char *)argv[2];
NSLog(@"Executing command |%s|", command);
system(command);
[pool drain];
return 0;
}
脚本:
/bin/launchctl load -S Aqua /Library/LaunchAgents/com.company.agent.plist