setenv variable with spaces in launchd.conf?

本秂侑毒 提交于 2019-11-30 12:43:58
Serge ETL

Try using launchd instead (create plist /Library/LaunchDaemons/java.props.plist):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" 
    "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>java.props</string>
    <key>ProgramArguments</key>
    <array>
        <string>launchctl</string>
        <string>setenv</string>
        <string>JAVA_OPTS</string>
        <string>-Djava.io.tmpdir=/tmp -Dfile.encoding=UTF-8</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
    <key>LaunchOnlyOnce</key>
    <true/>
</dict>
</plist>

This will run once and set your environment up. Hope it'll help.

It does not work as in a c-shell because /etc/launchd.conf is nothing but a sequence of special commands for launchctl. See the launchctl man page for a reference on what works in /etc/launchd.conf

Sadly, this still won't help you to solve this problem, but I hope it will clarify the context a little. To me this problem is a shortcoming in Apple's launchctl/launchd tools. I have a hard time working around it myself.

On 10.8.2 the following command works fine:

$ launchctl setenv MY_VARIABLE My\ value\ with\ spaces

Verify with:

$ launchctl getenv MY_VARIABLE
My value with spaces

Add the following line to /etc/launchd.conf (create if it doesn't exist)

setenv MY_VARIABLE My\ value\ with\ spaces

Note that this will only have an effect after rebooting.

To use the new value without having to reboot, additionally run the command in the terminal

launchctl setenv MY_VARIABLE My\ value\ with\ spaces
as patrikha suggested.

Note that this will only have an effect for applications started after running the command. Manipulating /etc/launchd.conf is still necessary to keep the change after rebooting.

In 10.13.2, this works for me:

launchctl setenv MY_VARIABLE 'My value with spaces'

But to be specific, I'm using Script Editor to create an app (~/StartupEnvVars.app) containing lines like this:

do shell script "launchctl setenv MY_VARIABLE 'My value with spaces'"

Then, in Settings|Users|Login Items I add this app as a startup item.

It works, with this caveat: if I have Terminal and my R app running when I log out, and choose to reopen windows on logging back in, after those apps auto load when I log in, they DO NOT see the environment variables set by StartupEnvVars.app. But, if I close those apps and reopen them, then they see the environment variables.

If it were Terminal only, I'd use .bash_profile, but I want to set env vars for use in R also. I think there have been about 5 ways to set global env vars in OSX since I started using it. Each major new version has a new way of doing it. Annoying.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!