[launchd-dev] Cause of "Service cannot load in requested session"?
James Bucanek
subscriber at gloaming.com
Fri Nov 13 16:03:44 PST 2015
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.
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. But at
least it will now start when the user does log in, and it continues to
run after they log out.
James
More information about the launchd-dev
mailing list