[launchd-dev] [Un]loading user agents on [Un]install

Tim Schooley tim at sbdev.net
Tue Sep 23 04:13:37 PDT 2008


Hi all,

So I think this is probably quite a "sane" task to have to perform:

    - Upon installing a product, detect which users are logged in, and load
the installed LaunchAgent script for each user.

    - Upon uninstalling the product, detect which users are logged in, and
unload the LaunchAgent script for each user.

These tasks would be performed preupgrade, postinstall, etc...

I wrote the following script to perform this task:


----
for user in `/usr/bin/users`
do
    echo "Loading agent for user $user..."
    /usr/bin/su -l $user -c "/bin/launchctl load -SAqua /Library/LaunchAgents/com.mycompany.myproduct.plist"
    EXIT_STATUS=$?
    if [ $EXIT_STATUS != 0 ]
    then
        echo "failed (non-fatal, status $EXIT_STATUS)."
    else
        echo "Done!"
    fi
done
----

And similarly for unloading.

The script works for the user that launched the installer. But if for
example another user is logged in using Fast-User Switching, the following
error is printed in the installer log when preupgrade attempts to unload
the script:

preupgrade[pid]: launchctl: Error unloading: com.mycompany.myproduct.plist

Also, on post[upgrade/install], the following error is printed when
attempting to load the script for other users:

postupgrade[pid]: launch_msg(): socket is not connected

I've searched around for answers to these issues, but had no luck. I was
hoping someone here might be able to point me in the right direction?

Many thanks for any help!

Kind regards,

Tim




More information about the launchd-dev mailing list