The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   UNIX - General (http://hintsforums.macworld.com/forumdisplay.php?f=16)
-   -   Set ENV variable at startup (http://hintsforums.macworld.com/showthread.php?t=104211)

Gnarlodious 08-08-2009 07:56 AM

Set ENV variable at startup
 
I have a 10.5 server (standard 10.5) that boots up to the login window. How would I set an environment variable that will be accessible to the shell for a Python webapp?

I tried adding the variable to /etc/launchd.conf but it does not work. The file is only run when I log into a user account, which I don't want to do.

Oddly, this is an extremely simple thing to do in Linux, but seems to be impossible in OSX.

hayne 08-08-2009 12:21 PM

1) What user account runs the "webapp" that you are asking about?
Why can't you set the env var in the shell setup files for that account? Or in the setup for whatever program runs that app - e.g. the web server.

2) See also the last part of the section on env vars in this Unix FAQ

Gnarlodious 08-08-2009 12:47 PM

First, this server is running Apache, so I guess user is www. I want a systemwide variable so I can invoke the script from ssh in additon to webpages.

Second, the ~/.MacOSX/environment.plist file only loads when the user has logged in, and I am running this machine headlessly (it stops booting at the login window). I can log in to the server ssh and see a whole list of environment variables, soI know it is possible somehow to set them.

I should also explain that I spent several hours yesterday searching for the solution.

hayne 08-08-2009 01:25 PM

1) See the Apache doc on setting env vars: http://httpd.apache.org/docs/2.2/env.html

2) When you use 'ssh', you are logging into a user account and hence the shell setup files for that account determine the environment.

3) You said that you tried setting the env var in "/etc/launchd.conf" but that it didn't work. That is surprising since the man pages indicate that this is the recommended mechanism and google shows others having had success with this:
http://www.google.com/search?q=launchd.conf+setenv
See for example: http://www.karma.net/html/index.php?...52144856072197

How exactly did you try to set the env var in launchd.conf ?
Note that the contents of launchd.conf should be commands of the form expected by 'launchctl' - see the man page: http://developer.apple.com/documenta...unchctl.1.html
And of course you need to reboot to see the effect of changes.

Gnarlodious 08-08-2009 02:18 PM

1) Apache may be launching the shell script, so internal Apache variables are not relevant.

2) I could set the variable from the bash profile, but it would be limited to that Terminal session.

3) Yes, it is confusing. I did set it up on the home (dev) computer and it worked as expected. But the exact same file and permissions on my server do not load. Unless I have logged into a (any) user account. Maybe it is a bug in launchd, because I thought launchd was the precursor of all processes.

hayne 08-08-2009 03:06 PM

Quote:

Originally Posted by Gnarlodious (Post 546134)
Apache may be launching the shell script, so internal Apache variables are not relevant.

The Apache docs say that this mechanism will affect programs launched via CGI etc - so it probably will affect your webapp - how is it launched?

Quote:

I could set the variable from the bash profile, but it would be limited to that Terminal session.
If you set the var in your shell config file(s), it will be active in any session after that. So it will certainly be active for future 'ssh' sessions.

Quote:

I did set it up on the home (dev) computer and it worked as expected. But the exact same file and permissions on my server do not load. Unless I have logged into a (any) user account. Maybe it is a bug in launchd, because I thought launchd was the precursor of all processes.
'launchd' is the precursor of all processes.
It's strange that it worked on your non-server machine but not on your server machine. Maybe this is a bug that affects only the OS X Server version of 'launchd' ? That seems strange since I don't imagine there is a difference in 'launchd' between OS X Server and OS X (client).

Gnarlodious 08-08-2009 06:21 PM

My server is running standard OSX (which I might add works wonderfully serving 5 websites). It is running the exact same OS as my home computer. The only difference is that the server stops booting at the login screen.

It must be a bug in the OS.

hayne 08-09-2009 09:54 AM

Quote:

Originally Posted by Gnarlodious (Post 546157)
My server is running standard OSX (which I might add works wonderfully serving 5 websites). It is running the exact same OS as my home computer. The only difference is that the server stops booting at the login screen.

It must be a bug in the OS.

The same OS will have the same behaviour in the same circumstances.
Hence there must be something different about your server machine setup that is causing the difference.

Have you checked the log files to see if there is anything relevant there?

Gnarlodious 08-11-2009 10:56 AM

A user on another forum has verified that launchd.conf is not loaded UNTIL a user has logged in. So I am out of luck. This despite numerous UNIX experts insisting that launchd.conf is loaded at boot time. Wonder why Apple goes off in these nonstandard directions.

SirDice 08-11-2009 11:43 AM

Quote:

Originally Posted by Gnarlodious (Post 546516)
A user on another forum has verified that launchd.conf is not loaded UNTIL a user has logged in.

That's not correct. Launchd is the first program being run when the system boots (on v10.4 and later).

http://developer.apple.com/documenta...5736-TPXREF104

Quote:

Wonder why Apple goes off in these nonstandard directions.
It's explained in the doc link I posted above.

hayne 08-11-2009 12:36 PM

Quote:

Originally Posted by Gnarlodious (Post 546516)
A user on another forum has verified that launchd.conf is not loaded UNTIL a user has logged in.

Please give details. Asserting that something does not work the way it is documented to work requires extreme evidence.

And didn't you already say that it does work on your home machine?

Quote:

Wonder why Apple goes off in these nonstandard directions.
"The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore all progress depends on the unreasonable man."
-- George Bernard Shaw

I.e. 'launchd' is a better way - and Apple hopes it will become standard. (It's open source.)

tw 08-11-2009 04:34 PM

Quote:

Originally Posted by Gnarlodious (Post 546516)
A user on another forum has verified that launchd.conf is not loaded UNTIL a user has logged in.

two different file locations here:
  • /etc/launchd.conf should load very early in the boot process, and be system-general.
  • ~/.launchd.conf (in a user's home folder) is user-specific, and would only be checked after user login.
I suspect that this 'user on another forum' (that'll teach you to use another forum!) has his head stuck up his home directory. ;)

biovizier 08-11-2009 10:28 PM

Has anybody here actually tried putting a 'setenv' statement in the "/private/etc/launchd.conf" file? Other than the OP, that is...

hayne 08-11-2009 11:33 PM

Quote:

Originally Posted by biovizier (Post 546591)
Has anybody here actually tried putting a 'setenv' statement in the "/private/etc/launchd.conf" file?

Since I had to reboot for the recent Safari update, I tried it.

I created the file /etc/launchd.conf using 'sudo vi' and inserted one 'setenv' line :
Code:

% cat /etc/launchd.conf
setenv CECI_NEST_PAS_UNE_PIPE cigar

% ls -l /etc/launchd.conf
-rw-r--r--  1 root  wheel  36 11 Aug 22:43 /etc/launchd.conf

Then I rebooted and verified that this env var was set in Terminal and in GUI apps as shown by my AppleScript showEnvVars

Code:

env | grep CECI
CECI_NEST_PAS_UNE_PIPE=cigar

And then, to check that the env var was being set before login, I edited the (pre-existing) StartupItems script for VirtualBox and added the following line at the top:
Code:

ConsoleMessage "CECI_NEST_PAS_UNE_PIPE=$CECI_NEST_PAS_UNE_PIPE"
I rebooted again and then used Console.app to look at system.log where I found the following line:
Code:

Aug 11 23:14:21 silverbook com.apple.SystemStarter[17]: CECI_NEST_PAS_UNE_PIPE=cigar
I note that the time in that message was well before the time when I logged in. So the /etc/launchd.conf mechanism for setting env vars is definitely working on my Mac which is running OS X 10.5.8

tw 08-11-2009 11:49 PM

I can duplicate Hayne's results, if not his mastery of French. I set up an /etc/launchd.conf that said setenv troubador wilco, and then wrote a launch daemon (placed in /Library/LaunchDaemons) which called a script that wrote 'wilco' to disk with a timestamp before my user login.

Gnarlodious 08-12-2009 11:07 PM

I want to thank you guys for all for your effort, but I am still not seeing the result I want. I log into my server as root and still don't get the variable I want. I have no doubt that StartupItems is reading the variable, but it is not accessible to root account via ssh. I suppose I could set the server to login to the GUI as root, but it would be rather insecure. And I shouldn't need to do it just to get an env variable.

hayne 08-12-2009 11:48 PM

Quote:

Originally Posted by Gnarlodious (Post 546747)
I am still not seeing the result I want. I log into my server as root and still don't get the variable I want. I have no doubt that StartupItems is reading the variable, but it is not accessible to root account via ssh.

I think it would be useful if you slowed down and told us - in excruciating detail - exactly what you are doing.
And even though you "have no doubt that StartupItems is reading the variable", it would be useful to test this by putting a "ConsoleMessage" to echo the value of the variable in whatever StartupItems script you are referring to.
But do tell us in full detail what you are doing, including showing us the results of 'cat /etc/launchd.conf' and 'ls -ld /etc'

jbc 08-13-2009 12:00 AM

I've not used ssh much, but I came across this thread that seems to be trying to resolve some of the same issues as this thread, I think. Maybe some of the solutions will help?

SirDice 08-13-2009 02:54 AM

Quote:

Originally Posted by Gnarlodious (Post 546747)
I have no doubt that StartupItems is reading the variable, but it is not accessible to root account via ssh. I suppose I could set the server to login to the GUI as root, but it would be rather insecure. And I shouldn't need to do it just to get an env variable.

How do you logon as root? Do you login using a normal account and then use sudo? If so, sudo strips quite a lot of environment variables. Have a look at the sudoers file.

If you login as root directy using ssh then why are you worried about logging in as root on the GUI?

Gnarlodious 08-13-2009 09:03 AM

Thanks for all the help, I have solved this problem expediently but not elegantly. Rather than use the same shell env var I have configured Apache to duplicate the value and will access it from the script. I still do not know how to set a shell var that user www can access.

hayne 08-13-2009 11:40 AM

Quote:

Originally Posted by Gnarlodious (Post 546788)
I still do not know how to set a shell var that user www can access.

If you show us the details of what you've tried (as I suggested in post #17), then we could perhaps see where you are going wrong when trying to use the /etc/launchd.conf mechanism.


All times are GMT -5. The time now is 05:32 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.