Making a custom Nudge launch agent


Introduction

Nudge releases generally come as Nudge, Nudge logger, and the Nudge launch agent. As of this writing, the Nudge launch agent will launch up Nudge every 30 minutes. The package is also signed:

If you’re creating your own launch agent, you may want to code-sign it, or you may not have to, depending on how you’re deploying the launch agent.

Here’s an example of how you can change the launch agent, in case you don’t want it to run as aggressively (or maybe you want it to run more aggressively!).

I’m writing this as a series of terminal commands, so it’s easy to reproduce, but you can certainly use a point-and-click tool for creating custom packages to do the same thing.

The bottom line is that you want to deliver the (modified) launch agent to the /Library/LaunchAgents directory, and then you want to run a postinstall script to load up the launch agent as the currently logged-in user.

Steps

Find a folder you want to use as your workspace. It could be something like ~/Documents/GitHub or ~/Downloads or ~/Desktop. For this example, I’m just going to use the desktop.

cd ~/Desktop

Make a clone of the munkipkg GitHub repo:

git clone git@github.com:munki/munki-pkg.git

Make a clone of the Nudge GitHub repo:

git clone git@github.com:macadmins/nudge.git

Since macOS 12.3, Apple removed /usr/bin/python, so you’ll have to install Python and use the Python you installed. For this example, I’m going to use Munki‘s Python.

Create a new empty munkipkg project:

/usr/local/munki/munki-python munki-pkg/munkipkg --create nudge_launch_agent

Create the necessary subdirectories within the new project you just created:

mkdir -p nudge_launch_agent/payload/Library/LaunchAgents
mkdir -p nudge_launch_agent/scripts

Copy the launch agent from the Nudge project clone to your new launch agent package project:

cp nudge/build_assets/com.github.macadmins.Nudge.plist nudge_launch_agent/payload/Library/LaunchAgents/

Copy the postinstall script from the Nudge project clone to your new launch agent package project:

cp nudge/build_assets/postinstall-launchagent nudge_launch_agent/scripts/postinstall

Open the launch agent .plist and edit it so that it’s not run every half hour:

open nudge_launch_agent/payload/Library/LaunchAgents/com.github.macadmins.Nudge.plist

If you don’t know how StartCalendarInterval works for launch agents, you may want to read Apple’s official documentation on launchd or MacOS launchd plist StartInterval and StartCalendarInterval examples.

Here are a couple of examples, though.

If you want Nudge to run twice a day, you can have the StartCalendarInterval look something like this:

   <key>StartCalendarInterval</key>
   <array>
      <dict>
         <key>Hour</key>
         <integer>9</integer>
         <key>Minute</key>
         <integer>0</integer>
      </dict>
      <dict>
         <key>Hour</key>
         <integer>16</integer>
         <key>Minute</key>
         <integer>0</integer>
      </dict>
   </array>

If you want Nudge to run once an hour, you do something like this instead:

   <key>StartCalendarInterval</key>
   <array>
      <dict>
         <key>Minute</key>
         <integer>0</integer>
      </dict>
   </array>

Once you’re done editing, go ahead and build your new package:

/usr/local/munki/munki-python munki-pkg/munkipkg nudge_launch_agent

You should now see a newly built package with your own custom launch agent for Nudge:

Now that you have this project created, you can skip all the earlier steps in the future. Any time you want to modify the launch agent again, just go back to this project, edit the launch agent, open the build-info.plist to increment the package version, and then use munkipkg to recreate a new launch agent package.


Leave a Reply

Your email address will not be published.