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

前端 未结 9 1393
爱一瞬间的悲伤
爱一瞬间的悲伤 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:27

    It is possible to set environment variables on Mac OS X 10.10 Yosemite with 3 files + 2 commands.

    Main file with environment variables definition:

    $ ls -la /etc/environment 
    -r-xr-xr-x  1 root  wheel  369 Oct 21 04:42 /etc/environment
    $ cat /etc/environment
    #!/bin/sh
    
    set -e
    
    syslog -s -l warn "Set environment variables with /etc/environment $(whoami) - start"
    
    launchctl setenv JAVA_HOME      /usr/local/jdk1.7
    launchctl setenv MAVEN_HOME     /opt/local/share/java/maven3
    
    if [ -x /usr/libexec/path_helper ]; then
        export PATH=""
        eval `/usr/libexec/path_helper -s`
        launchctl setenv PATH $PATH
    fi
    
    osascript -e 'tell app "Dock" to quit'
    
    syslog -s -l warn "Set environment variables with /etc/environment $(whoami) - complete"
    

    Service definition to load environment variables for user applications (terminal, IDE, ...):

    $ ls -la /Library/LaunchAgents/environment.user.plist
    -rw-------  1 root  wheel  504 Oct 21 04:37 /Library/LaunchAgents/environment.user.plist
    $ sudo cat /Library/LaunchAgents/environment.user.plist
    
    
    
    
        Label
        environment.user
        ProgramArguments
        
                /etc/environment
        
        KeepAlive
        
        RunAtLoad
        
        WatchPaths
        
            /etc/environment
        
    
    
    

    The same service definition for root user applications:

    $ ls -la /Library/LaunchDaemons/environment.plist
    -rw-------  1 root  wheel  499 Oct 21 04:38 /Library/LaunchDaemons/environment.plist
    $ sudo cat /Library/LaunchDaemons/environment.plist
    
    
    
    
        Label
        environment
        ProgramArguments
        
                /etc/environment
        
        KeepAlive
        
        RunAtLoad
        
        WatchPaths
        
            /etc/environment
        
    
    
    

    And finally we should register these services:

    $ launchctl load -w /Library/LaunchAgents/environment.user.plist
    $ sudo launchctl load -w /Library/LaunchDaemons/environment.plist
    

    What we get:

    1. The only place to declare system environment variables: /etc/environment
    2. Instant auto-update of environment variables after modification of /etc/environment file - just relaunch your application

    Issues / problems:

    In order your env variables were correctly taken by applications after system reboot you will need:

    • either login twice: login => logout => login
    • or close & re-open applications manually, where env variables should be taken
    • or do NOT use feature "Reopen windows when logging back".

    This happens due to Apple denies explicit ordering of loaded services, so env variables are registered in parallel with processing of the "reopen queue".

    But actually, I reboot my system only several times per year (on big updates), so it is not a big deal.

提交回复
热议问题