Re: Environment woes executing scripts from my OS X app
Re: Environment woes executing scripts from my OS X app
- Subject: Re: Environment woes executing scripts from my OS X app
- From: Clark Cox <email@hidden>
- Date: Wed, 16 Dec 2015 15:23:51 -0800
If you know the environment variables you want to set, just use /usr/bin/env. (that’s it’s raison d’être):
- Instead of executing:
path/to/script args
- Execute:
/usr/bin/env PYTHONPATH=… PYTHONHOME=... path/to/script args
Or you can use posix_spawn to do the launching, which allows you to specify environment variables as well
Or, if posix_spawn isn’t available, you can fork() and use execle()
Or if execle() isn’t available, you can fork(), change the environment in your child process and then use plain-old exec()
> On Dec 16, 2015, at 14:29, Rick Mann <email@hidden> wrote:
>
> Sorry, I didn't specify enough constraints in my original post.
>
> - The code that calls the external scripts has to be cross-platform, and run on linux as well as OS X. So, no NSTask.
> - I can modify the code that calls the scripts, and I can use OS X-only APIs to set up a global environment (for processes launched by my app) if that sort of thing is possible.
> - I don't want to make the user modify things like launchd config or /etc. They can install required dependencies (like certain Python modules), but that's about it. I'd love to find a way to package those in the app bundle, too.
>
> I'm thinking the best thing to do is pass in paths and an environment string to be used in the system() call (e.g., invoke it as "export PYTHONPATH=...; export PYTHONHOME=...; path/to/script args"). Maybe I can even leverage that to let me put the requisite python modules in the app bundle...
>
>> On Dec 16, 2015, at 11:07 , Paul Scott <email@hidden> wrote:
>>
>> Sorry, I messed up the script. It should have been this:
>>
>> set vars to { ¬
>> {name:"ANT_HOME", value:"/usr/local/apache-ant-1.9.6"}, ¬
>> {name:"CATALINA_HOME", value:"/Users/pscott/Projects/apache-tomcat-8.0.24"}, ¬
>> {name:"LAUNCHD_SCRIPT", value:"/Users/pscott/bin/logon_as"}, ¬
>> {done:true} ¬
>> }
>> repeat with i from 1 to (count of vars) - 1
>> do shell script ¬
>> "/bin/launchctl setenv " & name of item i of vars & ¬
>> " " & value of item i of vars
>> end repeat
>>
>> Paul
>>
>>> By the way, you could also use this AppleScript, saved as an application, and run automatically via the System Preferences -> Users & Groups -> Current User -> Login Items configuration.
>>>
>>> set vars to { ¬
>>> { name:"ANT_HOME", value:"/usr/local/apache-ant-1.9.6" }, ¬
>>> { name:"CATALINA_HOME", value:”/usr/local/apache-tomcat-8.0.24" }, ¬
>>> { done: true } ¬
>>> }
>>> repeat with i from 1 to count of vars - 1
>>> do shell script "/bin/launchctl " ¬
>>> & name of item i of vars & " " ¬
>>> & value of item 1 of vars
>>> end repeat
>>>
>>> That is much easier to set up, but has the disadvantage of bouncing the application icon in the Dock momentarily at login. The launchd mechanism is silent, and seems to complete a tad bit sooner.
>>>
>>> Paul
>>>
>>>> On Dec 16, 2015, at 15:47, Rick Mann <email@hidden> wrote:
>>>>
>>>>> I'm working on an OS X app that unfortunately has to call a series of bash and python scripts for part of the processing it does. I was able to include the scripts in my app's bundle, and invoke them there, but the environment is different when launched via my app than when launched on the command line. How can I best control the environment used when executing external scripts?
>>>>>
>>>>> --
>>>>> Rick Mann
>>>>> email@hidden
>>>>
>>>>
>>>> If you want to have specific environment variables set for all apps launched regardless of how they were launched, you can use the launchd mechanism, which is compatible with all the latest Mac OS X releases.
>>>>
>>>> You can put this file in ~/Library/LaunchAgents/local.launchdrc.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>local.launchdrc</string>
>>>> <key>Disabled</key>
>>>> <false/>
>>>> <key>RunAtLoad</key>
>>>> <true/>
>>>> <key>ProcessType</key>
>>>> <string>Background</string>
>>>> <key>ProgramArguments</key>
>>>> <array>
>>>> <string>/Users/yourusername/.launchdrc</string>
>>>> </array>
>>>> <key>StandardErrorPath</key>
>>>> <string>/dev/null</string>
>>>> <key>StandardOutPath</key>
>>>> <string>/dev/null</string>
>>>> </dict>
>>>> </plist>
>>>>
>>>> Then, create ~/.launchdrc (chmod 755) that looks something like this:
>>>>
>>>> #!/bin/sh
>>>> launchctl setenv ANT_HOME "/usr/local/apache-ant-1.9.6"
>>>> launchctl setenv CATALINA_HOME “/usr/local/apache-tomcat-8.0.24”
>>>>
>>>> Where each environment variable you want available to all launched apps is listed. Add variables as needed. This will set up an environment for the user at login time that will get picked up by all launched apps, whether run from the command line or launched via Finder.
>>>>
>>>> This replaces the old ~/.MacOSX mechanism where you could set environment variables at login.
>>>
>
>
> --
> Rick Mann
> email@hidden <mailto:email@hidden>
>
>
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden