Search code examples
cocoalaunchd

"launchctl start" not working in a launch daemon process


I have a daemon process, set up using launchd on Mac. The daemon is running as root. The following line does not launch my UI application -

system("launchctl start com.project.myproj");

But if I go to the terminal and run the same command -> launchctl start com.project.myproj. It launches my application. My question is am i doing something wrong or is system not behaving correctly ?


Solution

  • The problem is that launch daemons started by the system don't have access to Aqua, so your daemon process can't access the desktop environment.

    Apple's Technical Note TN2083: Daemons and Agents describes some of this. (See this section on launchd agents.)

    Someone asked a similar question about accessing the Aqua environment from an SSH connection.

    I'm guessing you need to re-architect your application a bit:

    1. Create a launch agent that runs in every user's Aqua session.
    2. Have the agent connect to your root daemon, or have the root daemon look for an instance of the launch agent.
    3. When signaled by the root daemon, have the launch agent launch the graphical part of the application.