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

Hi All, I have a shell script to find the latest version number from the head of SVN tree and this script is called from a perl routine. The script and perl routine works fine. However, the same perl script do not print anything when it is run under cgi. Here are files.
----------------------------------------------------------- find_latest_svn_version.sh #!/bin/sh -v SVNINFO="/home/buildmaster/bin/svn info" #find the latest SVN Version SVNVERSION=`${SVNINFO} --revision HEAD $1 | grep "Revision" | sed -e ' +s/Revision: \(.*\)/\1/'` echo $SVNVERSION ---------------------------------------------------------- a.pl #! /usr/bin/perl -w print "Content-type: text/html\n\n"; print <<END_SEC1; <html> <head> <title> Build DashBoard</title> </head> <body> starting </body> </html> END_SEC1 $findversion = "find-latest-svn-version.sh"; $srcdir = "/home/buildmaster/build/releases/sources/MPP_SW_BASE"; $scriptDir = "/home/buildmaster/build/scripts"; if( system( "$scriptDir/$findversion $srcdir") ) { print "<br>failed<br>\n"; } else { print "<br>succeeded<br>\n"; }
If I run a.pl, it prints the latest SVN Version. However, if I copy a.pl to cgi-bin directory and run it under cgi, I do not see the svn version gettting displayed. If I make a slight modification to find_latest_svn_version.sh removing the "--revision HEAD", I can see that it display the version number(though it is not the one from the HEAD of the tree). I could not figure out why adding this argument causes the script to behave differently. I am wondering whether there are tricks to in passing arguments correctly under cgi, as the same perl script works fine from command line. Any help is greatly appreciated and thanks in advance.

Replies are listed 'Best First'.
Re: Why does the same perl routine behave differently under cgi?
by moritz (Cardinal) on Mar 23, 2009 at 23:29 UTC
    In general there can be many reasons why scripts behave differently under CGI than on the normal user shell:
    • Permissions: CGI scripts are run under a user with low privilges. Operations that succeed for normal users can fail for the www-data user, or whatever it's called. This is probably the most commonly observed difference.
    • Environment variables: CGI scripts usually have far less of the "usual" environment variables set
    • Security context: on selinux enabled systems, CGI scripts are run in a different security context that forbids certain operations even if normal permissions would allow them (like opening a socket, writing to files in a home dir). This is probably the hardest difference to find if you're not aware that such a thing exists.
    • Output interpretation: usually the output of a CGI script is interpreted by a browser, whereas the user often looks at the output of a shell script directly.
Re: Why does the same perl routine behave differently under cgi?
by fzellinger (Acolyte) on Mar 23, 2009 at 23:52 UTC
    All of the checkpoints by moritz are strategically correct, but here are some tactical things to do to help debugging:

  • I assume that your webserver is properly configured (can you run other scripts?) and that your file/directory permissions are correct.
  • Compare your environments both in the shell (env|sort) and perl (print Dumper(\%ENV);) and compare the results from when you are logged in as yourself versus when the script is called by the webserver.
  • Make sure you can see the webserver access and error logs and know how to interpret messages there (or at least post them to perlmonks next time).
  • Your version of SVN is probably compiled against various shared libraries. These shared libraries may be installed in /usr/local/lib which is in your environmental variable LDPATH or LD_LIBRARY_PATH. However this env variable may be set to something different for the webserver process calling your perl script. I have frequently had problems like the one you are describing, and have discovered that the webserver error logs show that libraries like libapr, libapr_util, libiconv, libneon, etc could not be found when the webserver spawned process tried running SVN. SVN uses a lot of "non-core" (to the OS) libraries.
      Thanks for all the quick responses. I will try to print the ENV parameters and look into the weblogs
Re: Why does the same perl routine behave differently under cgi?
by ikegami (Patriarch) on Mar 23, 2009 at 23:09 UTC

    the same perl script do not print anything when it is run under cgi.

    Really? Not even "failed" or "succeeded"?

      It was my bad. Yes. It prints Starting Succeeded. But not the SVN Version