[launchd-dev] Cause of "Service cannot load in requested session"?

Gregory Neagle gregneagle at mac.com
Fri Nov 13 16:07:38 PST 2015


> On Nov 13, 2015, at 4:03 PM, James Bucanek <subscriber at gloaming.com> wrote:
> 
> Greetings,
> 
> I finally had a chance to run this to ground. Here's what I discovered:
> 
> Basically, LimitLoadToSessionType=Background is broken. (bug report #23536982)
> 
> I was able to narrow down the problem to this simple example. First, write the following plist file to ~/Library/LaunchAgents/info.test.agent.plist (edit the path, if you want to play along at home):
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
> <plist version="1.0">
> <dict>
> <key>Label</key>
> <string>info.test.agent</string>
> <key>LimitLoadToSessionType</key>
> <string>Background</string>
> <key>ProgramArguments</key>
> <array>
> <string>/usr/bin/touch</string>
> <string>/Users/james/Desktop/TestAgentDidRun.txt</string>
> </array>
> <key>RunAtLoad</key>
> <true/>
> </dict>
> </plist>
> 
> Now, restart the computer. What *should* happen is /usr/bin/touch should execute when the background session for the user is created, creating the /Users/james/Desktop/TestAgentDidRun.txt file.

When do you expect this session to be created? There has to be some trigger. I’d expect it only in the case of a login of _some_ sort — via CLI or SSH (and maybe also GUI)

> 
> What actually happens is nothing. The agent is never started and the file is never created. The behavior is the same in 10.10 and 10.11.
> 
> When the Aqua session is finally created, you still get this message in the console:
> 
> Nov 12 22:14:41 Jamess-Mac-Pro otherbsd[261]: Failed to bootstrap agent: path = /Users/james/Library/LaunchAgents/info.test.agent.plist, error = 134: Service cannot load in requested session
> 
> But, as previously discussed, this just the agent getting loaded into the Aqua session, and failing because it can't.
> 
> I was also able to confirm Damien's workaround of manually reloading the service once the Aqua session is created. I have another Aqua-only GUI agent. I've modified it so that it looks to see if my (Background) scheduler process is running. If it isn't running, but is installed, it reloads it by executing the following command:
> 
>    launchctl bootstrap user/<uid> ~/Library/LaunchAgents/com.qrecall.scheduler.plist
> 
> That starts the scheduler process in the background session.
> 
> This doesn't completely solve my problem, because what I *really* want is for the Background agent to start before the user logs in.

I don’t see how that can happen. Consider the case of a machine connected to a network directory service containing hundreds or thousands of users. Should the OS start background sessions for every user in the directory service?

> But at least it will now start when the user does log in, and it continues to run after they log out.

I think that is best you can hope for, unless you create a LaunchDaemon that can find out what users you want to start background sessions for.

> 
> James
> _______________________________________________
> launchd-dev mailing list
> launchd-dev at lists.macosforge.org
> https://lists.macosforge.org/mailman/listinfo/launchd-dev



More information about the launchd-dev mailing list