mivk
mivk

Reputation: 15049

How to access environment variables in launchd plist

I have a launchd per-user agent. In it's .plist, I would like to use the $HOME environment variable.

Is it possible?

(it is the "Program" key, which I would like to define as "$HOME/bin/myscript")

Upvotes: 8

Views: 3603

Answers (2)

Lri
Lri

Reputation: 27633

EnableGlobbing enables tilde and wildcard expansion for ProgramArguments (but not Program).

<key>EnableGlobbing</key>
<true/>
<key>ProgramArguments</key>
<array>
    <string>~/bin/myscript</string>
</array>

ProgramArguments can only be an array of strings and not just a string. Tilde expansion also works in WatchPaths by default.

Upvotes: 5

Gordon Davisson
Gordon Davisson

Reputation: 126108

launchd doesn't perform any substitutions on the values in its .plists, so this can't be done in the form you're trying to do it. What you can do is hand the command you want to run to a shell, and let it perform the variable substitutions and run the command. For instance, you could replace that Program key with this:

<key>ProgramArguments</key>
<array>
    <string>/bin/sh</string>
    <string>-c</string>
    <string>exec $HOME/tmp/myscript</string>
</array>

(Note that the exec prefix isn't really necessary, it's just a minor optimization. It makes the shell replace itself with the script, rather than starting the script as a subprocess and then waiting around for it to finish.)

Upvotes: 6

Related Questions