How to extend PATH of LaunchAgents in ~/Library/LaunchAgents

launchdzsh

I noticed that ~/.zshrc is not loaded… I need to add /usr/local/bin to PATH.

Best Answer

How to extend PATH of LaunchAgents in ~/Library/LaunchAgents?

You can use the following for example:

<key>EnvironmentVariables</key>
<dict>
    <key>PATH</key>
    <string>/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/sbin</string>
</dict>

Add it to your XML Plist file in an appropriate manner so it doesn't break the other key groups.

I'd place it at the top of the XML Plist file, for example:

<?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>PATH</key>
        <string>/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/sbin</string>
    </dict>

Then the rest of your XML Plist file.


Notes:

  • If my XML Plist file in ~/Library/LaunchAgents is calling a shell script, I typically set the PATH (path) in my scripts as well as at times use the fully qualified pathname of the executable.

  • When using Launch Agents and Launch Daemons, I highly recommend reading the manual pages for launchctl, launchd.plist and launchd.

    • You can read the manual page for command in Terminal by typing command and then right-click on it and select: Open man Page