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

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

    [Original answer]: You can still use launchctl setenv variablename value to set a variable so that is picked up by all applications (graphical applications started via the Dock or Spotlight, in addition to those started via the terminal).

    Obviously you will not want to do this every time you login.

    [Edit]: To avoid this, launch AppleScript Editor, enter a command like this:

    do shell script "launchctl setenv variablename value"
    

    (Use multiple lines if you want to set multiple variables)

    Now save (+s) as File format: Application. Finally open System SettingsUsers & GroupsLogin Items and add your new application.

    [Original answer]: To work around this place all the variables you wish to define in a short shell script, then have a look at this previous answer about how to run a script on MacOS login. That way the the script will be invoked when the user logs in.

    [Edit]: Neither solution is perfect as the variables will only be set for that specific user but I am hoping/guessing that may be all you require.

    If you do have multiple users you could either manually set a Login Item for each of them or place a copy of com.user.loginscript.plist in each of their local Library/LaunchAgents directories, pointing at the same shell script.

    Granted, neither of these workarounds is as convenient as /etc/launchd.conf.

    [Further Edit]: A user below mentions that this did not work for him. However I have tested on multiple Yosemite machines and it does work for me. If you are having a problem, remember that you will need to restart applications for this to take effect. Additionally if you set variables in the terminal via ~/.profile or ~/.bash_profile, they will override things set via launchctl setenv for applications started from the shell.

    0 讨论(0)
  • 2020-11-22 08:20

    What did work for me (inspired from aax' thanks) :

    Paste this into /Library/LaunchDaemons/com.apple.launchd.limit.plist then reboot :

    <?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>eicar</string>
      <key>ProgramArguments</key>
      <array>
        <string>/bin/launchctl</string>
        <string>limit</string>
        <string>maxfiles</string>
        <string>16384</string>
        <string>16384</string>
      </array>
      <key>RunAtLoad</key>
      <true/>
      <key>ServiceIPC</key>
      <false/>
    </dict>
    </plist>
    

    If you need it step by step :

    • Launch terminal
    • Type sudo su then enter your password to log in as root
    • Type vi /Library/LaunchDaemons/com.apple.launchd.limit.plist
    • When into the vi editor, press the key i to enter insert mode then paste the exact code content above (⌘+v). This will force the limit to 16384 files per process and 16384 files total
    • Save your file and quit using esc then :wq
    • Reboot your system, and check that it is working using the command launchctl limit

    I hope this helped you.

    0 讨论(0)
  • 2020-11-22 08:21

    Cited from

    Apple Developer Relations 10-Oct-2014 09:12 PM

    After much deliberation, engineering has removed this feature. The file /etc/launchd.conf was intentionally removed for security reasons. As a workaround, you could run launchctl limit as root early during boot, perhaps from a LaunchDaemon. (...)

    Solution:

    Put code in to /Library/LaunchDaemons/com.apple.launchd.limit.plist by bash-script:

    #!/bin/bash
    
    echo '<?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>eicar</string>
            <key>ProgramArguments</key>
            <array>
                    <string>/bin/launchctl</string>
                    <string>limit</string>
                    <string>core</string>
                    <string>unlimited</string>
            </array>
            <key>RunAtLoad</key>
            <true/>
            <key>ServiceIPC</key>
            <false/>
    </dict>
    </plist>' | sudo tee /Library/LaunchDaemons/com.apple.launchd.limit.plist
    
    0 讨论(0)
  • 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
    <?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>environment.user</string>
        <key>ProgramArguments</key>
        <array>
                <string>/etc/environment</string>
        </array>
        <key>KeepAlive</key>
        <false/>
        <key>RunAtLoad</key>
        <true/>
        <key>WatchPaths</key>
        <array>
            <string>/etc/environment</string>
        </array>
    </dict>
    </plist>
    

    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
    <?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>environment</string>
        <key>ProgramArguments</key>
        <array>
                <string>/etc/environment</string>
        </array>
        <key>KeepAlive</key>
        <false/>
        <key>RunAtLoad</key>
        <true/>
        <key>WatchPaths</key>
        <array>
            <string>/etc/environment</string>
        </array>
    </dict>
    </plist>
    

    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.

    0 讨论(0)
  • 2020-11-22 08:30

    You can give https://github.com/ersiner/osx-env-sync a try. It handles both command line and GUI apps from a single source and works withe the latest version of OS X (Yosemite).

    You can use path substitutions and other shell tricks since what you write is regular bash script to be sourced by bash in the first place. No restrictions.. (Check osx-env-sync documentation and you'll understand how it achieves this.)

    I answered a similar question here where you'll find more.

    0 讨论(0)
  • 2020-11-22 08:36

    I added the variables in the ~/.bash_profile in the following way. After you are done restart/log out and log in

    export M2_HOME=/Users/robin/softwares/apache-maven-3.2.3
    export ANT_HOME=/Users/robin/softwares/apache-ant-1.9.4
    launchctl setenv M2_HOME $M2_HOME
    launchctl setenv ANT_HOME $ANT_HOME
    export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Users/robin/softwares/apache-maven-3.2.3/bin:/Users/robin/softwares/apache-ant-1.9.4/bin
    launchctl setenv PATH $PATH
    

    NOTE: without restart/log out and log in you can apply these changes using;

    source ~/.bash_profile
    
    0 讨论(0)
提交回复
热议问题