IT

launchd.conf를 통한 환경 변수 설정이 OS X Yosemite / El Capitan / macOS Sierra / Mojave에서 더 이상 작동하지 않습니까?

lottoking 2020. 5. 16. 10:23
반응형

launchd.conf를 통한 환경 변수 설정이 OS X Yosemite / El Capitan / macOS Sierra / Mojave에서 더 이상 작동하지 않습니까?


launchd.conf더 이상 내 환경 변수를로드하지 않는 것 같습니다 . 다른 사람이 알아 차렸습니까?

환경 변수를 영구적으로 설정하는 또 다른 솔루션이 있습니까?


이 내용으로 environment.plist파일을 작성하십시오 ~/Library/LaunchAgents/.

<?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즉시 시작할 수도 있습니다 .

[편집하다]

El Capitan에서도 동일한 솔루션이 작동합니다.

Xcode 7.0+는 기본적으로 환경 변수를 평가하지 않습니다. 이 명령으로 이전 동작을 활성화 할 수 있습니다.

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

[편집하다]

이것이 제대로 작동하지 않는 몇 가지 상황이 있습니다. 컴퓨터가 다시 시작되고 "다시 로그인 할 때 창 다시 열기"를 선택하면 다시 열린 창에 변수가 표시되지 않을 수 있습니다 (에이전트가 실행되기 전에 열려있을 수 있음). 또한 ssh를 통해 로그인하면 변수가 설정되지 않으므로 ~ / .bash_profile에서 변수를 설정해야합니다. 마지막으로 El Capitan 및 Sierra의 PATH에서는 작동하지 않는 것 같습니다. 'launchctl config user path ...'및 / etc / paths를 통해 설정해야합니다.


[ 원래 답변 ] : 모든 응용 프로그램 (터미널을 통해 시작된 것 외에 Dock 또는 Spotlight를 통해 시작된 그래픽 응용 프로그램)에 launchctl setenv variablename value의해 선택되도록 변수를 설정하는 데 계속 사용할 수 있습니다 .

분명히 로그인 할 때마다이 작업을 수행하지 않을 것입니다.

[ 편집 ] :이를 피하려면을 실행 AppleScript Editor하고 다음과 같은 명령을 입력하십시오.

do shell script "launchctl setenv variablename value"

(여러 변수를 설정하려면 여러 줄을 사용하십시오)

이제 파일 형식 (Application )으로 ( + s)를 저장하십시오 . 마지막으로 사용자 및 그룹로그인 항목을 열고 새 응용 프로그램을 추가하십시오.System Settings

[ 원래 답변 ] :이 문제를 해결하려면 짧은 쉘 스크립트에서 정의하려는 모든 변수를 확인한 다음 MacOS 로그인에서 스크립트를 실행하는 방법에 대한 이전 답변을 살펴보십시오 . 그렇게하면 사용자가 로그인 할 때 스크립트가 호출됩니다.

[ 편집 ] : 변수가 특정 사용자 에게만 설정되므로 솔루션이 완벽 하지는 않지만 필요한 모든 것을 기대하고 추측합니다.

여러 사용자가있는 경우 각 사용자 에 대해 로그인 항목수동으로 설정 하거나 동일한 쉘 스크립트를 가리키는 각 로컬 Library / LaunchAgents 디렉토리 com.user.loginscript.plist 사본을 배치 할 수 있습니다 .

물론 이러한 해결 방법 중 어느 것도 /etc/launchd.conf 만큼 편리하지 않습니다 .

[ 추가 편집 ] : 아래 사용자는 이것이 효과가 없다고 언급합니다. 그러나 여러 요세미티 머신에서 테스트했으며 나에게 적합합니다. 문제가있는 경우 적용하려면 응용 프로그램을 다시 시작해야합니다. 또한 ~ / .profile 또는 ~ / .bash_profile을 통해 터미널에 변수를 설정 하면 쉘에서 시작된 응용 프로그램에 대해 launchctl setenv통해 설정 한 변수 가 재정의됩니다 .


Mac OS X 10.10 Yosemite에서 3 개의 파일 + 2 개의 명령으로 환경 변수를 설정할 수 있습니다.

환경 변수 정의가있는 기본 파일 :

$ 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"

사용자 애플리케이션 (터미널, 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>

루트 사용자 애플리케이션에 대한 동일한 서비스 정의 :

$ 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.


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

Here are the commands to restore the old behavior:

# create a script that calls launchctl iterating through /etc/launchd.conf
echo '#!/bin/sh

while read line || [[ -n $line ]] ; do launchctl $line ; done < /etc/launchd.conf;
' > /usr/local/bin/launchd.conf.sh

# make it executable
chmod +x /usr/local/bin/launchd.conf.sh

# launch the script at startup
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>launchd.conf</string>
  <key>ProgramArguments</key>
  <array>
    <string>sh</string>
    <string>-c</string>
    <string>/usr/local/bin/launchd.conf.sh</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
</dict>
</plist>
' > /Library/LaunchAgents/launchd.conf.plist

Now you can specify commands like setenv JAVA_HOME /Library/Java/Home in /etc/launchd.conf.

Checked on El Capitan.


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.


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.


The solution is to add your variable to /etc/profile. Then everything works as expected! Of course you MUST do it as a root user with sudo nano /etc/profile. If you edit it with any other way the system will complain with a damaged /etc/profile, even if you change the permissions to root.


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

참고URL : https://stackoverflow.com/questions/25385934/setting-environment-variables-via-launchd-conf-no-longer-works-in-os-x-yosemite

반응형