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.
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.)