SMAppService how to set Agent/Daemon

I’m developing an app on macOS where I want a daemon/agent to run only when the user explicitly triggers it using SMAppService. The goal is to start the daemon when needed, perform the required task, and then stop/unload it after completion. Additionally, if unload or unregister calls are missed, I want to ensure that the daemon doesn’t start automatically upon reboot. Instead, it should only run when a specific action is taken. What are the best practices and detailed methods for achieving this clean and controlled behavior?

Daemons and agents are managed by launchd. That uses an on-demand model: A job advertises a service and only starts when someone requests that service.

I want a daemon/agent to run only when the user explicitly triggers it using SMAppService.

This suggests you’ve missed a key point here. SMAppService is about registering and unregistering jobs. Once the job is registered, its started and stopped state is controlled by demand on its services.

Finally, are you sure you want a daemon or agent for this? Based on your description, it sounds like you might be better off with an XPC service. A daemon only makes sense if you need you need elevated privileges [1]. An agent only makes sense if you want the job to continue running after your app has quit [2]. If neither of those apply, an XPC service might work for you. And XPC services don’t even require registration. The system automatically makes them available to your app.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] Well, there are other cases where a daemon makes sense, but your post makes it clear that you’re not interested in them.

[2] Ditto.

SMAppService how to set Agent/Daemon
 
 
Q