通过 launchd.conf 设置环境变量不再适用于 OS X Yosemite/El Capitan/macOS Sierra/Mojave? [英] Setting environment variables via launchd.conf no longer works in OS X Yosemite/El Capitan/macOS Sierra/Mojave?

查看:35
本文介绍了通过 launchd.conf 设置环境变量不再适用于 OS X Yosemite/El Capitan/macOS Sierra/Mojave?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看起来 launchd.conf 不再加载我的环境变量了.其他人注意到了吗?

It looks like the launchd.conf does not load my environment variable anymore. Has anyone else noticed that?

还有其他永久设置环境变量的解决方案吗?

Is there another solution to permanently set environment variables?

推荐答案

~/Library/LaunchAgents/中创建一个environment.plist文件,内容如下:p>

Create an environment.plist file in ~/Library/LaunchAgents/ with this content:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>my.startup</string>
  <key>ProgramArguments</key>
  <array>
    <string>sh</string>
    <string>-c</string>
    <string>
    launchctl setenv PRODUCTS_PATH /Users/mortimer/Projects/my_products
    launchctl setenv ANDROID_NDK_HOME /Applications/android-ndk
    launchctl setenv PATH $PATH:/Applications/gradle/bin
    </string>

  </array>
  <key>RunAtLoad</key>
  <true/>
</dict>
</plist>

您可以在 <string></string> 块内添加许多 launchctl 命令.

You can add many launchctl commands inside the <string></string> block.

plist 将在系统重启后激活.您也可以使用 launchctl load ~/Library/LaunchAgents/environment.plist 立即启动它.

The plist will activate after system reboot. You can also use launchctl load ~/Library/LaunchAgents/environment.plist to launch it immediately.

同样的解决方案也适用于 El Capitan.

The same solution works in El Capitan too.

Xcode 7.0+ 默认不评估环境变量.可以使用以下命令启用旧行为:

Xcode 7.0+ doesn't evaluate environment variables by default. The old behaviour can be enabled with this command:

默认写入 com.apple.dt.Xcode UseSanitizedBuildSystemEnvironment -bool NO

在某些情况下,这并不完全奏效.如果重新启动计算机并选择重新登录时重新打开窗口",重新打开的窗口可能看不到变量(可能在代理运行之前打开它们).此外,如果您通过 ssh 登录,则不会设置变量(因此您需要在 ~/.bash_profile 中设置它们).最后,这似乎不适用于 El Capitan 和 Sierra 上的 PATH.这需要通过 'launchctl config user path ...' 和/etc/paths 进行设置.

There a couple of situations where this doesn't quite work. If the computer is restarted and "Reopen windows when logging back in" is selected, the reopened windows may not see the variables (Perhaps they are opened before the agent is run). Also, if you log in via ssh, the variables will not be set (so you'll need to set them in ~/.bash_profile). Finally, this doesn't seem to work for PATH on El Capitan and Sierra. That needs to be set via 'launchctl config user path ...' and in /etc/paths.

这篇关于通过 launchd.conf 设置环境变量不再适用于 OS X Yosemite/El Capitan/macOS Sierra/Mojave?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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