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

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

问题描述

看起来像 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 这个内容:

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>

您可以添加许多 launchctl < string>< / string> 块。

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:

defaults write com.apple.dt.Xcode UseSanitizedBuildSystemEnvironment -bool NO

defaults write com.apple.dt.Xcode UseSanitizedBuildSystemEnvironment -bool NO

有几种情况哪里不行。如果计算机重新启动,并且重新打开Windows,则重新打开的窗口可能看不到变量(也许它们在代理程序运行之前打开)。此外,如果您通过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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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