Reproducing execution launchd execution environment

Launchd jobs run with a default shell that does not import any of the typical environment variables you'll see in an interactive shell; in particular the PATH variable lacks any directories that your interactive shell might have added, which can cause headaches. You can work around that two ways:

  • By making your script as thoroughly generic as possible: using full path specifications to every command and utility, and explicitly setting variables as you need them rather than relying on the environment.
  • By adding an EnvironmentVariables dictionary to your launchd plist file and specifying the environment there.

The second looks like this:

<?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>EnvironmentVariables</key>
    <dict>
           <key>VARIABLE_NAME_1</key>
           <string>variable_contents</string>
           <key>VARIABLE_NAME_2</key>
           <string>variable_contents</string>
    </dict>
    < ... >
</dict>
</plist>