如何在启动的plist中访问环境变量 [英] How to access environment variables in launchd plist

查看:68
本文介绍了如何在启动的plist中访问环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按用户启动的代理.在它的.plist中,我想使用 $ HOME 环境变量.

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

有可能吗?

(这是程序"键,我想将其定义为"$ HOME/bin/myscript")

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

推荐答案

launchd不会对其.plists中的值执行任何替换,因此无法以您尝试执行的形式进行.您可以做的是将要运行的命令交给shell,然后让它 it 执行变量替换并运行该命令.例如,您可以将 Program 键替换为此:

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>

(请注意,实际上并没有必要使用 exec 前缀,这只是次要的优化.它使shell用脚本替换自身,而不是将脚本作为子进程启动,然后等待才能完成.)

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

这篇关于如何在启动的plist中访问环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆