[launchd-dev] Creating a Launch Agent (per user) on demand when client app is open

Piotr Panasewicz piotr.panasewicz at mac.com
Thu Jan 15 04:25:44 PST 2015


Hello,

I would like to create a Launch Agent that would start on demand when my bundle app requires it. I have it currently implemented like below which monitors a specific path for existence of a file. This works ok for one user logged in. If I there are more users logged via Fast User Switching the OS tends to open the service for different user, not the one that’s currently using my app. That’s because two users are now monitoring the same path and the OS isn’t sure in which user’s context the service should run.

Also my bundle app has LSMultipleInstancesProhibited set to true, so it isn’t allowed to run two instances at once. But if User A opens my app and there is a User B logged in, the service may open for User B (even the file from the plist was created by User A and User B hasn’t touched my app).

Therefore I need a solution that would allow my client app to open the service for the same user it runs as. And if I close the app for User A, switch to User B and start the client, the service would now open for User B.

Thanks,
Peter

<?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>KeepAlive</key>
    <dict>
        <key>PathState</key>
        <dict>
            <key>/Users/Shared/Company/Service.txt</key>
            <true/>
        </dict>
    </dict>
	<key>Label</key>
	<string>com.company.service</string>
	<key>Program</key>
	<string>/Users/Shared/Company/Service</string>
	<key>MachServices</key>
	<dict>
		<key>com.company.service</key>
		<true/>
	</dict>
</dict>
</plist>



More information about the launchd-dev mailing list