Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access environment variables in launchd plist

Tags:

macos

launchd

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")

like image 831
mivk Avatar asked May 25 '12 08:05

mivk


2 Answers

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.)

like image 84
Gordon Davisson Avatar answered Oct 16 '22 11:10

Gordon Davisson


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.

like image 33
Lri Avatar answered Oct 16 '22 12:10

Lri