How to start/stop my launch agent in all logged in sessions from a root user process?

huangapple go评论46阅读模式
英文:

How to start/stop my launch agent in all logged in sessions from a root user process?

问题

我正在为我的启动守护程序编写安装程序,以及一个在macOS上每个已登录用户会话中运行的启动代理程序。安装程序将以管理员身份运行,因此我可以像这样安装和启动我的启动守护程序:

launchctl load /Library/LaunchDaemons/com.example.MyDaemon.plist

并像这样停止它:

launchctl unload /Library/LaunchDaemons/com.example.MyDaemon.plist

问题是,我需要在每个已登录用户会话中在安装(和卸载)后立即运行(和停止)我的启动代理程序,因为似乎直到我注销并重新登录之前,它不会自动发生。

我将我的启动代理程序放在:

/Library/LaunchAgents/com.example.MyAgent.plist

但如何从根用户进程启动/停止它呢?

PS. 两个 plist 文件的 RunAtLoadKeepAlive 都设置为 true

英文:

I'm writing an installer for my launch daemon, and for a launch agent, that will run in every logged in user session on macOS. The installer will be running as an admin, and thus I can install and start my launch daemon as such:

launchctl load /Library/LaunchDaemons/com.example.MyDaemon.plist

and stop it as such:

launchctl unload /Library/LaunchDaemons/com.example.MyDaemon.plist

The issue is that I need to run (and stop) my launch agent right after installation (and un-installation) in every logged in user session, as it doesn't seem to happen automatically until I log off and log back in.

I placed my launch agent into:

/Library/LaunchAgents/com.example.MyAgent.plist

But how do I start/stop it from a root user process?

PS. Both plists have RunAtLoad and KeepAlive set to true.

答案1

得分: 1

我意识到这并不是你直接问题的确切回答,但可能解决了你潜在的问题。看起来你有一个启动守护程序和 N 个用户会话运行的启动代理程序,而且可能这些启动代理程序已经通过某种 IPC 机制(XPC?)与启动守护程序进行了检查。通过这个 IPC 通道,你可以让中央启动守护程序向代理实例发送一条消息,导致它们退出。根据退出代码和启动 plist,launchd 将重新启动它们或不重新启动。确切的逻辑何时发生由你决定,当然你要小心不要陷入重新启动循环。(当然,这并不能解决安装后初始代理启动的问题。)

英文:

I realise this is not a direct answer to the exact question you're asking, but may solve your underlying problem. It sounds like you have a launch daemon and N launch agents for N user sessions running, and presumably the launch agents have checked in with the launch daemon via some IPC mechanism (XPC?)? Via this IPC channel, you could have the central launch daemon send out a message to the agent instances which causes them to exit. Depending on the exit code and the launchd plist, launchd will either restart them or not. The exact logic as to when this happens is up to you and you'll want to be careful not to end up in a restart loop of course. (This doesn't solve the initial agent startup after installation though.)

huangapple
  • 本文由 发表于 2023年2月27日 00:23:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75573361.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定