ctfd
ctfd

Reputation: 348

How to source a file from launchctl

I'm trying to source a file from a launch agent, but it's unclear how to accomplish that (or if it's even possible). I know it's easy to setenv for single variables, but I need to bring in a bunch of them, so source is what I need.

<?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>my.sourcerer</string>
    <key>ProgramArguments</key>
    <array>
        <string>source</string>
        <string>my_file.sh</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>WorkingDirectory</key>
    <string>/path/to/source</string>
</dict>
</plist>

This loads and starts without any issue it seems:

$ launchctl load ~/Library/LaunchAgents/my.sourcerer.plist
$ launchctl start ~/Library/LaunchAgents/my.sourcerer.plist

But when I printenv none of my sourced environment variables show up. If manually I do:

$ source /path/to/source/my_file.sh
$ printenv

Then all of the variables show up. Why doesn't the launchctl agent seem to load the variables into my environment?

Upvotes: 0

Views: 1173

Answers (1)

Kurtis Rader
Kurtis Rader

Reputation: 7469

Based on the comments to the question I'm going to go out on a limb and try to answer the question.

It seems you want to launch a program when you login rather than manually after you open your first terminal session. The solution is to create a script that includes the necessary source command to initialize the environment. Then make that script pathname the first arg of your ~/Library/LaunchAgents/my.sourcerer.plist config.

You could also statically set those vars via launchctl setenv. But I would not recommend that approach because it obviously isn't dynamic. That is, if the output of the sourced script ever changes the statically set env vars inherited by every process would not change.jj

Upvotes: 1

Related Questions