Setting environment variables via launchd.conf no longer works in OS X Yosemite/El Capitan/macOS Sierra/Mojave?

前端 未结 9 1379
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 08:07

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

9条回答
  •  迷失自我
    2020-11-22 08:40

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

    
    
    
    
      Label
      my.startup
      ProgramArguments
      
        sh
        -c
        
        launchctl setenv PRODUCTS_PATH /Users/mortimer/Projects/my_products
        launchctl setenv ANDROID_NDK_HOME /Applications/android-ndk
        launchctl setenv PATH $PATH:/Applications/gradle/bin
        
    
      
      RunAtLoad
      
    
    
    

    You can add many launchctl commands inside the block.

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

    [Edit]

    The same solution works in El Capitan too.

    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

    [Edit]

    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.

提交回复
热议问题