[launchd-dev] My Launch Agent is Persisting Beyond Logout

Mac QA macqaguy at gmail.com
Tue Mar 11 13:41:12 PDT 2008


Hi,

I have a custom process that is invoked when a user logs in via having
a launchd config file located in /Library/Launch Agents. The config
file looks something like this:

	<key>KeepAlive</key>
	<true/>
	<key>Label</key>
	<string>com.company.mylaunchagent</string>
	<key>OnDemand</key>
	<false/>
	<key>ProgramArguments</key>
	<array>
		<string>/mylaunchagent</string>
	</array>
	<key>RunAtLoad</key>
	<true/>

The mylaunchagent is invoked just fine. If I do a fast user switch to
login another user then a second instance is invoked for the second
user. All seems good. However, when the second user logs out the
second instance of mylaunchagent continues to stay running even though
the user logs out!

The Apple Multiple User Environments document [1] makes two statements
regarding this. First, "If a user process daemonizes itself prior to
logout, it can live past the end of the user logout and prolong the
existence of the login session." And Second, "Per-user services are
shut down automatically and are not given the chance to abort the
logout procedure." The mylaunchagent process is always a background
process just invoked by launchd, so given the first statement I guess
that is why it is persisting logout. However, given the second
statement, since this is a per-user service as launchagents are by
definition, I don't understand why it isn't shut down automatically on
logout. Frankly, those two statements seem contradictory.

[1] Apple Multiple User Environments
http://developer.apple.com/documentation/MacOSX/Conceptual/BPMultipleUsers/Concepts/SystemContexts.html#//apple_ref/doc/uid/20002208-106206

Can anyone point me towards how to make sure my launch agent processes
get properly killed when a user logs out? I appreciate any tips.
Thanks!


More information about the launchd-dev mailing list