Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a CGI script running on a UNIX box. I need it to be able to access Environment variables (set in the .profile of the user apache runs as). I can do this command line by: my $testvariable = $ENV{TESTVAR}. This works command line, I can see this when I print it, however I would like to know how to achieve the same thing via CGI, when I run the script as a CGI page the $testvariable is empty.
  • Comment on Access system ENV variables from within CGI Script

Replies are listed 'Best First'.
Re: Access system ENV variables from within CGI Script
by Corion (Patriarch) on Oct 12, 2007 at 10:59 UTC

    Use the technique demonstrated in Get default login environment. Your .profile is only executed when you log in via the shell, not when your CGI program is run, even if it is run under your user account.

      Thanks for the link, I am using this code, tillys updated function actually. The shell we are using is ksh. Within the .profile I see many variables which are exported, but sadly do not get printed (using Data::Dumper) when I am testing my script with the tillys function. Any suggestions would be great. TIA

        You will need to debug the output from the `env` command then. Compare what the script outputs when it is run from your shell account. Compare what `env` returns when run from your shell account to the output from the CGI. Take out all intermediaries. Eliminate all differences. Find out where the two scripts/environments differ.

        Most likely, from your .profile something else gets run. Maybe only if there is a terminal connected. It's hard to tell without seeing your code and the relevant parts of the .profile.

Re: Access system ENV variables from within CGI Script
by cdarke (Prior) on Oct 12, 2007 at 12:24 UTC
    How is Apache started? .profile only gets executed by ksh if it is a login shell (then it executes .kshrc). So, for example, if it is started at system boot time that is not a login shell.
    On ksh88 non-login shells execute .kshrc only, but on ksh93 only interactive shells execute .kshrc.
    So, depending on the version of ksh you are using, you might be able to use .kshrc, but I can't guarantee it.
    To find the version of ksh: set -o emacs, then <CTRL>V.
      Thanks for the tip, following your advice the version number reports as being Version M-11/16/88i.
      apache is started ./apachectl start, sorry, I forgot to mention this.
Re: Access system ENV variables from within CGI Script
by ikegami (Patriarch) on Oct 12, 2007 at 14:39 UTC

    A much simpler solution is to use the following as your CGI script:

    #!/bin/sh --login exec /path/to/real/script.cgi $@

    Untested.

Re: Access system ENV variables from within CGI Script
by perlfan (Parson) on Oct 12, 2007 at 16:34 UTC
    Your environment in an interactive shell and the environment under which CGI runs is wholly different. Instead of trying to set variables on the outside of your script (i.e., via the actual environment), I suggest setting them explicitly in a Perl module that is shared by all of your Perl CGI code.
Re: Access system ENV variables from within CGI Script
by arcnon (Monk) on Oct 12, 2007 at 14:00 UTC
    a cgi script runs as wwwrun or www or whatever. Which is why it can't get at a "user" .profile.

    From my understanding the .profile was a config file that isn't loaded until you launch a terminal session and unloaded on close. Usually the profile.local is the global profile.

    Perhaps the better question is what you are trying to glean from the file that might be gleaned thur another way.

      A .profile file is not a "config file". It is an executable program which is run by a shell language interpreter. The only way to faithfully reproduce arbitrary settings made via executing a .profile shell program is by running that shell program or by writing a shell program interpreter. Of course, simple settings can be extracted via crafty regular expressions.

        wow I always thought that /etc/profile.d was the exe and that when run by the terminal loaded/ran the .profile.
Re: Access system ENV variables from within CGI Script
by UnstoppableDrew (Sexton) on Oct 12, 2007 at 18:55 UTC
    You also may need to explicitly add stuff to the Apache environment for your cgi script to see it. There's a directive name SetEnv or something like that (been a while since I looked). You will need to add any variables you want available to your cgi script that aren't already there.