{"id":38,"date":"2013-05-04T12:56:00","date_gmt":"2013-05-04T02:56:00","guid":{"rendered":""},"modified":"2018-05-22T23:51:23","modified_gmt":"2018-05-22T13:51:23","slug":"using-environment-plist-with-mountain-lion","status":"publish","type":"post","link":"https:\/\/pbw.id.au\/blog\/2013\/05\/using-environment-plist-with-mountain-lion\/","title":{"rendered":"Using environment.plist with Mountain Lion"},"content":{"rendered":"<div>\n<div>\n<div><b>UPDATE<\/b><br \/>\n<b>This post is now obsolete. For the preferred method in both Mountain Lion and Mavericks, see\u00a0 <a href=\"http:\/\/www.pbw.id.au\/blog\/uncategorized\/setting-environment-variables-in-os-x-yosemite-and-mavericks\/\">Setting environment variables in OS X Mountain Lion and Mavericks<\/a>.<\/b> With Mountain Lion (OS X 10.8), the environment settings from <i>~\/.MacOSX\/environment.plist<\/i> are not taken into account when the background system environment is set up by <i>launchd<\/i>, the OS X initialisation process. Consequently, if you want settings like <i>$JAVA_HOME<\/i> to be available to Java applications you start from Finder, you must find an alternative for setting them.<\/div>\n<p><!--more--><br \/>\nThe beauty of the <i>environment.plist<\/i> method was that, by setting the variables in the plist from within your <i>.profile<\/i>, you could keep the shell and system environments in sync. There are obvious advantages in this synchronisation. If you are like me, you have gone to a lot of trouble to put such synchronisation in place. See my previous post, <a href=\"http:\/\/www.pbw.id.au\/blog\/uncategorized\/setting-environment-variables-in-os-x-lion\/\">Setting Environment Variables in OS X Lion<\/a>.<\/p>\n<p>The now-recommended method of system variable setting is to use the <i>setenv<\/i> subcommand of <i>launchctl<\/i>, like so:<\/p>\n<pre>launchctl setenv M2 \/usr\/share\/maven\/bin<\/pre>\n<p>Such commands can be saved in the file <i>\/etc\/launchd.conf,<\/i> in which case only the subcommand is saved:<\/p>\n<pre>setenv M2 \/usr\/share\/maven\/bin<\/pre>\n<p>You can check the state of the <i>launchd<\/i> environment with the subcommand <i>export<\/i>:<\/p>\n<pre>launchctl export<\/pre>\n<p>This will output a series of shell commands of the form<\/p>\n<pre>M2=\" \/usr\/share\/maven\/bin\"; export M2;<\/pre>\n<p>This serves as a test for any attempts to set the <i>launchd<\/i> environment variables.<\/p>\n<p>There are three problems with using <i>launchctl<\/i> in this way. Firstly, <i>\/etc\/launchd.conf<\/i> is a system file (when it exists, which it does not by default), and it is in a directory (<i>\/etc<\/i>) which is writable only by root. Yes, you can <i>sudo<\/i> to edit the file, but then you face the second problem, touched on above. You have two discrete sources for environment variable settings, which can easily get out of sync. Lastly, the strings defined in setenv statements like the above, must be string literals. There is no means of resolving variable content based on other variables. For example, you cannot define<\/p>\n<pre>setenv TMPDIR $HOME\/tmp<\/pre>\n<p>The problem, then, is to find a way to dynamically set the <i>launchd<\/i> environment, based on your existing <i>.profile<\/i> and <i>.MacOSX\/environment.plist<\/i> settings. Another bonus from this approach is that, within you .profile, you can use all of the shell facilities to define the content of variables.<\/p>\n<div>When the system starts, the <i>launchd<\/i> process finds system daemon processes to launch from <i>\/System\/Library\/LaunchDaemons &amp;<\/i> \/Library\/LaunchDaemons. When a user logs in, <i>launchd<\/i> looks for user agents to start up in <i>\/System\/Library\/LaunchAgents<\/i>, <i>\/Library\/LaunchAgents<\/i> &amp; <i>~user\/Library\/LaunchAgents<\/i>. While the first two are system-owned directories, the third is owned by the individual user.<\/div>\n<div>We need two files: 1) a shell file which will actually issue the <i>launchctl setenv<\/i> commands, and 2) a plist file that will tell <i>launchd<\/i> where to find the executable, and how to run it. The shell file is available <a href=\"http:\/\/pbw.id.au\/plist2lctl\/plist2launchctl\">here<\/a>; the plist file is available <a href=\"http:\/\/pbw.id.au\/plist2lctl\/au.id.pbw.plist2launchctl.plist\">here<\/a>.<\/div>\n<h4>The executable: plist2launchctl<\/h4>\n<pre style=\"font-size: 14px;\"><span style=\"color: red;\">#!\/bin\/sh<\/span>\n\nPLIST=\"$HOME\/.MacOSX\/environment.plist\"\nSLEEP_TIME=10\n<span style=\"color: red;\"># Uncomment following to echo launchctl commands to stdout<\/span>\n<span style=\"color: red;\"># Key StandardOutPath will have to be set in the plist file<\/span>\n<span style=\"color: red;\"># (au.id.pbw.plist2launchctl) for output to be captured.<\/span>\n<span style=\"color: red;\">#ECHO_TO_STDOUT=true<\/span>\n\none_cmd () {\n    eval \"$@\"\n}\n\n[[ -e \"$SHFILE\" ]] &amp;&amp; {\n    rm \"$SHFILE\"\n    [[ $? -eq 1 ]] &amp;&amp; {\n        echo \"$SHFILE\" cannot be removed. &gt;&amp;2\n        exit 1\n    }\n}\n\ncmd_list=`cat \"$PLIST\" | \nsed -En \n'\/^[[:space:]]*&lt;key&gt;(.*)&lt;\/key&gt;[[:space:]]*\/ {\ns\/\/launchctl setenv 1 \/\nN\n}\n\/^.*&lt;string&gt;.*[[:space:]].*&lt;\/string&gt;.*$\/ {\nd\n}\n\/^(launchctl .*)n[[:space:]]*&lt;string&gt;(.+)&lt;\/string&gt;[[:space:]]*$\/ {\ns\/\/12\/\np\nd\n}'`\n\n[[ -n \"$ECHO_TO_STDOUT\" ]] &amp;&amp; echo \"$cmd_list\"\n\necho \"$cmd_list\" | while read line; do one_cmd $line; done\n\n[[ -n \"$ECHO_TO_STDOUT\" ]] &amp;&amp; echo Sleeping in plist2launchctl\n\n<span style=\"color: red;\"># Sleep for a while so launchd doesn't get upset<\/span>\n[[ -n \"$SLEEP_TIME\" ]] &amp;&amp; sleep \"$SLEEP_TIME\"<\/pre>\n<p><i>Notes:<\/i><br \/>\nThe output of the whole of the <i>cat|sed<\/i> pipeline is captured in the <i>cmd_list<\/i> variable by enclosing the lot in backquotes. The <i>sed<\/i> command is itself enclosed in single quotes. It simply reads the contents of <i>environment.plist<\/i> line at a time, and merges key\/string pairs into the corresponding <i>launchctl setenv<\/i> commands, storing them in <i>cmd_list<\/i>. <i>cmd_list<\/i> is then itself read line at a time, and each line is handed to <i>eval<\/i> for execution.<br \/>\n<i>Permissions &amp; Location:<\/i><br \/>\nTo stay on the safe side, give the file -rwxr-xr-x permissions. It should be placed in <i>\/usr\/libexec<\/i>, which is root owned. You will have to use <i>sudo<\/i> to copy it.<\/p>\n<\/div>\n<h4>The plist file: au.id.pbw.plist2launchctl<\/h4>\n<div>\n<pre style=\"font-size: 14px;\">&lt;? xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n&lt;!DOCTYPE plist PUBLIC \"-\/\/Apple\/\/DTD PLIST 1.0\/\/EN\" \"http:\/\/www.apple.com\/DTDs\/PropertyList-1.0.dtd\"&gt;\n&lt;plist version=\"1.0\"&gt;\n    &lt;dict&gt;\n        &lt;key&gt;Label&lt;\/key&gt;\n        &lt;string&gt;au.id.pbw.plist2launchctl&lt;\/string&gt;\n        &lt;key&gt;KeepAlive&lt;\/key&gt;\n        &lt;false\/&gt;\n        &lt;key&gt;Program&lt;\/key&gt;\n        &lt;string&gt;\/usr\/libexec\/plist2launchctl&lt;\/string&gt;\n        &lt;key&gt;RunAtLoad&lt;\/key&gt;\n        &lt;true\/&gt;\n        &lt;key&gt;UserName&lt;key&gt;\n        &lt;string&gt;pbw&lt;\/string&gt;\n    &lt;\/dict&gt;\n&lt;\/plist&gt;<\/pre>\n<div><i>Notes:<\/i><br \/>\nThe <i>UserName<\/i> field will, of course, be set to your own login name.<br \/>\nIf you need to debug the plist file, add the following lines.<\/p>\n<pre style=\"font-size: 14px;\">        &lt;key&gt;StandardOutPath&lt;\/key&gt;\n        &lt;string&gt;\/Users\/pbw\/plist.log&lt;\/string&gt;\n        &lt;key&gt;Debug&lt;\/key&gt;\n        &lt;true\/&gt;<\/pre>\n<\/div>\n<div>These lines are used in conjunction with the ECHO_TO_STDOUT variable in the executable file. If used, the <i>StandardOutPath<\/i> will point to a writable file on your system.<\/div>\n<div><i>Permissions &amp; Location:<\/i><\/div>\n<div>Again, give the file -rwxr-xr-x permissions. Place it in your <i>$HOME\/Library\/LaunchAgents<\/i> directory.<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>UPDATE This post is now obsolete. For the preferred method in both Mountain Lion and Mavericks, see\u00a0 Setting environment variables in OS X Mountain Lion and Mavericks. With Mountain Lion (OS X 10.8), the environment settings from ~\/.MacOSX\/environment.plist are not taken into account when the background system environment is set up by launchd, the OS &hellip; <a href=\"https:\/\/pbw.id.au\/blog\/2013\/05\/using-environment-plist-with-mountain-lion\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Using environment.plist with Mountain Lion&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[27],"tags":[],"class_list":["post-38","post","type-post","status-publish","format-standard","hentry","category-observations"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8SCfl-C","jetpack-related-posts":[{"id":39,"url":"https:\/\/pbw.id.au\/blog\/2013\/05\/setting-environment-variables-in-os-x-lion\/","url_meta":{"origin":38,"position":0},"title":"Setting Environment Variables in OS X Lion","author":"pbw","date":"Fri 3rd May '13","format":false,"excerpt":"If you want to set environment variables in OS X in such a way as to be recognised in applications run from Finder, it is not enough to set the env var in .profile. \u00a0You must also ensure that the variables are set in the file ~\/.MacOSX\/environment.plist. Setting values in\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/pbw.id.au\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":34,"url":"https:\/\/pbw.id.au\/blog\/2013\/11\/setting-environment-variables-in-os-x-yosemite-and-mavericks\/","url_meta":{"origin":38,"position":1},"title":"Setting environment variables in  MacOS Big Sur","author":"pbw","date":"Thu 7th Nov '13","format":false,"excerpt":"This method uses launchctl to manage environment variables for programs invoked directly from Finder. \u00a0See the launchctl man page, especially the section LEGACY SUBCOMMANDS. \u00a0It's not entirely accurate, but that's not unusual. \u00a0The critical subcommands are getenv, setenv, and unsetenv. The man page indicates that the export subcommand is available;\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/pbw.id.au\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":87,"url":"https:\/\/pbw.id.au\/blog\/2011\/11\/close-encounters\/","url_meta":{"origin":38,"position":2},"title":"Close Encounters","author":"pbw","date":"Sat 12th Nov '11","format":false,"excerpt":"\u00a0 Close Encounters of the Third Kind was released in 1977, and was a blockbuster success for Steven Spielberg. Here's a\u00a0thumbnail sketch of the plot. A team of investigators find, intact in the Gobi Desert, a flight of Navy planes which disappeared in the 1940's, and\u00a0interview a witness to the\u2026","rel":"","context":"In &quot;Faith&quot;","block_context":{"text":"Faith","link":"https:\/\/pbw.id.au\/blog\/category\/belief\/faith\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":31,"url":"https:\/\/pbw.id.au\/blog\/2015\/03\/help-for-digest-checking\/","url_meta":{"origin":38,"position":3},"title":"Help for digest checking","author":"pbw","date":"Fri 20th Mar '15","format":false,"excerpt":"Updated 2018-02-14 It's pretty important to check the digests of software you download. \u00a0When a downloaded file is accompanied by a signature file, for example a gnupg .asc file, you can verify the signature with various tools. \u00a0Often though, a download site will include the MD5 or SHA1 digest hash\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/pbw.id.au\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":96,"url":"https:\/\/pbw.id.au\/blog\/2008\/01\/a-visit-to-medjugorje\/","url_meta":{"origin":38,"position":4},"title":"A visit to Medjugorje","author":"pbw","date":"Wed 30th Jan '08","format":false,"excerpt":"In the autumn of 1996 I took a ferry from Ancona across the Adriatic to Split. There was a reasonable swell, into which we heaved through the night. I slept little in the \"aircraft style\" seats, keeping an eye on my knapsack, in which all of my travelling possessions, and\u2026","rel":"","context":"In &quot;Faith&quot;","block_context":{"text":"Faith","link":"https:\/\/pbw.id.au\/blog\/category\/belief\/faith\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1100,"url":"https:\/\/pbw.id.au\/blog\/2023\/05\/peace-mongering-ukraine-style\/","url_meta":{"origin":38,"position":5},"title":"Peace-Mongering, Ukraine style","author":"Me","date":"Wed 17th May '23","format":false,"excerpt":"First published at NewCatallaxy blog, December 10, 2022. News Reports and Analysis Daily Mail, 30th November, 2021 The Daily Mail reported that three gatherings of some Downing Street staff had taken place during November and December of 2020. This was the lifting of the lid on the cesspool of cynicism\u2026","rel":"","context":"In &quot;GeoPolitics&quot;","block_context":{"text":"GeoPolitics","link":"https:\/\/pbw.id.au\/blog\/category\/geopolitics\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/pbw.id.au\/blog\/wp-json\/wp\/v2\/posts\/38","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pbw.id.au\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pbw.id.au\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pbw.id.au\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/pbw.id.au\/blog\/wp-json\/wp\/v2\/comments?post=38"}],"version-history":[{"count":8,"href":"https:\/\/pbw.id.au\/blog\/wp-json\/wp\/v2\/posts\/38\/revisions"}],"predecessor-version":[{"id":632,"href":"https:\/\/pbw.id.au\/blog\/wp-json\/wp\/v2\/posts\/38\/revisions\/632"}],"wp:attachment":[{"href":"https:\/\/pbw.id.au\/blog\/wp-json\/wp\/v2\/media?parent=38"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pbw.id.au\/blog\/wp-json\/wp\/v2\/categories?post=38"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pbw.id.au\/blog\/wp-json\/wp\/v2\/tags?post=38"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}