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

I have a CGI script, it works well for what it's supposed to do. Now as an update to the processing it does I need to call a second script which is writting in ksh.
The ksh script make use of environment variables, and runs the .profile inside. When run stand alone it works fine. When I call it via system it does not. Is there an easy to understand guide on how to resolve this so the CGI page can call the ksh script and have it work the same way it does when run stand alone?
  • Comment on Call ksh script from CGI with environment variables

Replies are listed 'Best First'.
Re: Call ksh script from CGI with environment variables
by haukex (Archbishop) on Jul 08, 2016 at 11:51 UTC

    Hi Anonymous,

    It would be best if you could provide a short, runnable example of the CGI script and a small sample ksh script that reproduces the problem - see Short, Self Contained, Correct Example - otherwise we are left to guess at the problem ("it does not work").

    Have you verified that the environment variables your ksh script is trying to access are also available to the Perl CGI script, for example by printing $ENV{VARNAME}? I suspect the environment variables made available to the CGI script by the webserver are different from the ones available in the shell, maybe that is the source of your problem.

    Regards,
    -- Hauke D

Re: Call ksh script from CGI with environment variables
by FreeBeerReekingMonk (Deacon) on Jul 08, 2016 at 15:38 UTC
    Most of the CGI's run with a small set of environment variables, are jailed (may not access that directory) or run as another user (nobody?) and can not access your .profile.

    If you run it as yourself, your .profile is readable. So check: access to each and every directory in the path (must have rx) and access to the .profile (must have r). Usually the permisions are given to the group, including a chgrp is done to a group that your CGI user can access (add whoami >/tmp/who.log to your script_name.ksh)

    so what you can do is run your ksh like this:

    $out = `sh -x /mypath/script_name.ksh 2>&1 >> /tmp/debug.log`;

    This will log all stdout and stderr to debug.log, where you can peruse the output for clues.