How to access environment variables in launchd plist

空扰寡人 提交于 2021-02-18 22:10:54

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!