Search code examples
macoslaunchdlaunch-agent

stopping LaunchAgent when uninstalling package


I build an app which provides LaunchAgent (an small UI app with "tray" icon; let's say that defined in /Library/LaunchAgents/foo.plist.

When installing package launchctl loads this LaunchAgent description automatically and starts processes for all logged-in users. (i.e i don't have to load it using launchctl load like i have to do with LaunchDaemons).

When i uninstall package, all files - including LaunchAgent plist file in /Library/LaunchAgents are removed but processes are still running.

(i have several users logged in, so there are several instances of this agent)

Now, how should i tell root launchd to stop these processes (for unspecified number of users) when running as root ? (note, launchctl ran as root doesn't even see those agents).

(pid-files,killall more-or-less-unique-executable-name is last resort and i'm keeping these solution as last resort)


Solution

  • Try this:

    LOGGEDUSERS=`who | grep console | awk '{ print $1 }'`
    for CURRUSER in $LOGGEDUSERS
    do
        su -l $CURRUSER -c 'launchctl unload /Library/LaunchAgents/your.plist'
    done
    

    In my uninstaller script there is also killall -15 <app_name> after that code (just in case)