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

Hi I am not able to get the unix environment variables through my CGI application. The webserver is apache2.0 The variables work properly at the command prompt. But they fail when it runs through apache2. Can anyone help me with regards sasi

Replies are listed 'Best First'.
Re: Perl Environment Variable
by davido (Cardinal) on Jan 30, 2004 at 05:14 UTC
    It sounds like the environment under which your process is running doesn't have $ENV{"HOME"} defined (in other words the "HOME" environment variable isn't set). But it's hard to say because you didn't actually bother to explain what "it's not working!" means, so I can only guess. Did you get some error message that might mean something to one of us? Are you running under the "use warnings;" pragma so that if $ENV{"HOME"} is undefined you'll get a warning? That would be helpful.

    Update: When I say "It sounds like the environment under which your process is running doesn't have $ENV{"HOME"} defined...", I'm saying that it's probable that your spawned process is running under a different environment instance (ie, as a different user). Thus, even though you think HOME is set, it's not set in the environment under which your job is running. Turning warnings on might tell you that the hash element you're looking for isn't defined. Now you know one possible reason why.


    Dave

      Hi, Thanks for ur comments The script runs perfectly when i run it from command line. It's able to get those environment variables. I have an another perl script whic is a cron process which would initiate my first script to do some action. In this case its not able to get those environment variables. sasi
        Oh, sure it's working. The Perl program *is* getting the environment variable. However, the variable isn't set. Programs run from cron have hardly any environment variables set. And that makes sense. "HOME" is something associated with a user logged on. A program run from cron isn't started by a user - so there's no "HOME" environment variable.

        You either need to set it explicitely yourself, or you should write your program in such a way you don't need the variable.

        Abigail

        Cron tasks are not shell sessions. You don't have the same environment. Your process has only the barest minimum environment setup, and that doesn't include $ENV{HOME} or other trivial niceties made by your .cshrc / .bashrc or other profile stuff.

        --
        [ e d @ h a l l e y . c c ]

Re: Perl Environment Variable
by etcshadow (Priest) on Jan 30, 2004 at 06:37 UTC
    (Building on your replies to the above questions:) you have to set up your environment for cron jobs... it doesn't just come for free.

    Two easy ways to do this:

    • in some versions of cron (linux's vixie cron, for one), there is a facility to just write out the environment at the top of the crontab.
    • more robust, and more cross-platform supprted: you can just source your shell rc/profile file before the command in the cron. Example (for bash... substitute your own shell as appropriate):
      0 7 * * * . ~/.bash_profile ; perl eat_breakfast.pl 0 12 * * * . ~/.bash_profile ; perl eat_lunch.pl 0 18 * * * . ~/.bash_profile ; perl eat_dinner.pl

    P.S. (update) This doesn't actually have anything to do with perl... it has to do with how environment variables are set and inherited, and how crond fits in with all that... if you want to see a really clear example of that, just put this in your crontab:

    * * * * * env > ~/cron_env
    And then see what's there (not much... certainly not as much you usually have in your shell, thanks to your rc/profile/.login file(s)).
    ------------ :Wq Not an editor command: Wq
Re: Perl Environment Variable
by coec (Chaplain) on Jan 30, 2004 at 07:53 UTC
    I hope you haven't called your Perl script script. This is really bad practice under Unix as there is aleady a utility called script.
    Try to avoid names like script and test as it will save you much heart-ache later.
Re: Perl Environment Variable
by Roger (Parson) on Jan 30, 2004 at 05:24 UTC
    Are you running the other script under the same shell? May be your other script has modified the HOME environment variable. It would certainly help if you show us your code, as davido said.

      Hi, I am getting a list of files in a text area when i get this text area using the param function i got it as a scalar variable. now i need it to be seperated in to an array based on the newline in the text area can anyone tell me how to do it. i tried it with split i was not able to acive it Thank you sasi kumar
        Maybe the cron-process is running as another user who doesn't have a HOME-environment variable set and your script "inherits" its environment (just guessing here as I have no knowledge of Sun/Solaris).

        Did you try checking whether the cron-process has access to the Home-environment variable?

        CountZero

        "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law