问题
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")
回答1:
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.)
回答2:
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.
来源:https://stackoverflow.com/questions/10751321/how-to-access-environment-variables-in-launchd-plist