MacOS – Launch agent loading question

launchdmacos

I have the following launch agent that runs on OS X and runs the below script to delete an app when a user downloads an OS upgrade from the App store. It then displays a message to a user after it is deleted.

#!/bin/bash

Version=$(sw_vers | grep ProductVersion | tail -c 7 | cut -d . -f 2)    

if [[ $Version -ge 11 ]]
then  
    launchctl unload /Library/LaunchAgents/net.company_name.blocksierra.plist
    rm -f /Library/LaunchAgents/net.company_name.blocksierra.plist
    rm -f /usr/local/bin/blocksierra.sh
exit 0

else
    rm -rf /Applications/Install\ macOS\ Sierra\ Public\ Beta.app/
    osascript -e 'display dialog "macOS sierra is not allowed on computers at this time." with title "Technology Notice" buttons {"OK"} default button "OK" giving up after 30'
fi

I am now going to package it and then deploy it to all of our Mac machines. My question is what do I add to the script to get the package to auto "load" the launchagent automatically once the package is installed?

So in other words how do I incorporate this command?

sudo launchctl load /Library/LaunchAgents/net.company_name.blocksierra.plist

Here is the plist

<?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>net.company_name.blocksierra</string>
<key>ProgramArguments</key>
<array>
    <string>/usr/local/bin/blocksierra.sh</string>
</array>
<key>KeepAlive</key>
<dict>
    <key>PathState</key>
    <dict>
        <key>/Applications/Install macOS Sierra Public Beta.app/</key>
        <true/>
    </dict>
</dict>
<key>OnDemand</key>
<true/>
</dict>
</plist>

Best Answer

All jobs related to items installed by packages (i.e. a pkg installer) are done with preflight or postflight scripts which are included in the package.

A preflight script may check if a needed folder exists and if its permissions are set properly. A postflight script may load launch agents and daemons installed by the pkg installer and set their owners and permissions.

In your case you would use a postflight script like this one:

#!/bin/bash

sudo /bin/launchctl load /Library/LaunchAgents/net.company_name.blocksierra.plist

BTW: Using if [[ $Version -ge 11 ]] will unload and delete the launch agent when El Capitan is installed. You may have to change this to 12.