通过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?
问题描述
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>
您可以在<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:
defaults write com.apple.dt.Xcode UseSanitizedBuildSystemEnvironment -bool NO
defaults write com.apple.dt.Xcode UseSanitizedBuildSystemEnvironment -bool NO
在几种情况下,这并不奏效.如果重新启动计算机并选择重新登录时重新打开窗口",则重新打开的窗口可能看不到变量(也许在运行代理之前已将其打开).另外,如果您通过ssh登录,则不会设置变量(因此您需要在〜/.bash_profile中进行设置).最后,这似乎不适用于El Capitan和Sierra上的PATH.需要通过"launchctl config用户路径..."和/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屋!