Pre And Postinstall Scripts

Greg Neagle edited this page May 14, 2018 · 10 revisions

Using pre and postinstall_scripts to avoid repackaging


A common reason to repackage software is to perform additional configuration tasks or install additional items. You can often avoid repackaging by creating a separate package with your changes or additions and marking that package as an update_for the original package.

Another technique to avoid repackaging is to add a preinstall_script and/or a postinstall_script to the pkginfo for an item. These scripts can take care of some of the tasks you may have previously needed to resort to repackaging to implement.


TextWrangler, a popular text editor, is distributed as a "drag-n-drop" disk image containing the TextWrangler application. But on first launch of the TextWrangler application, the user is prompted for admin credentials so some command-line tools can be installed. One approach to avoid this prompt is to repackage TextWrangler so that the applications and command-line tools are installed up front. But this can quickly become tedious as newer versions of TextWrangler are released.

An alternative is to implement a postinstall_script that copies the command-line tools from the TextWrangler bundle to their intended locations. Here's an example of such a postinstall_script embedded into the pkginfo for TextWrangler 3.5.3:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "">
<plist version="1.0">
    <string>Free text editor from the makers of BBEdit</string>

mkdir -p -m 775 /usr/local/bin
cp /Applications/ /usr/local/bin/
cp /Applications/ /usr/local/bin/
cp /Applications/ /usr/local/bin/

mkdir -p -m 775 /usr/local/share/man/man1
cp /Applications/ /usr/local/share/man/man1/
cp /Applications/ /usr/local/share/man/man1/
cp /Applications/ /usr/local/share/man/man1/

exit 0

Since these scripts are embedded in XML, you must escape certain characters that are special to the XML parser. Specifically, the following characters and their corresponding XML entities:

  • & is replaced with &amp;
  • < is replaced with &lt;
  • > is replaced with &gt;

You may want to use makepkginfo to do the escaping for you.

If your pre- or postinstall_script is in a file, you can generate the embedded version of the script, complete with proper escaping, with makepkginfo like so:

makepkginfo --postinstall_script /path/to/a/script
makepkginfo --preinstall_script /path/to/a/script

Additionally, GUI editing tools like MunkiAdmin, MunkiWebAdmin, and the like don't require this escaping if you aren't editing raw plists, as they do the escaping when converting a string into XML.

Failure of the preinstall_script will abort the installation attempt. Failure of the postinstall_script will log errors, but the install will be considered complete.


echo statements will be logged to /Library/Managed Installs/Logs/ManagedSoftwareUpdate.log


Munki 3

Managed Software Center

Command-line Tools

Munki configuration




Advanced Munki

Related tasks


Product-specific notes

Legacy Documentation

Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.