This is one of those "it shouldn't do that" kind of things. I've got a script that works fine when run as user www from a shell prompt, but fails when called via CGI (which also runs as www). Instead of generating a GIF file, the system command just returns an empty string and does nothing. It does not appear to be a problem with: the command itself, string interpolation, failure to execute the command, or user permissions. My best theory is, when running as a CGI, there is a subtle change between the string I put in backticks, and the command that actually gets passed to the system, probably related to argument parsing. But I can't pin down exactly what's happening. Evidence in support of this: 1) The script works perfectly from a shell prompt (both C and bourne), under the same user id as the web server uses. 2) Earlier backtick statements work just fine, under both shell and CGI. 3) When I remove all the arguments from the command I'm passing, I get the "help screen" output, as expected. 4) If I copy the string that is put in backquotes, and paste it into a shell window, it runs just fine. 5) I've tried using the system() command in place of backticks, both passing the entire string in and separating out the individual arguments as separate strings, with no change. This is the snippet that is behaving so strangely.
srand (time ^ $$ ^ unpack "%32L*", `ps axww | gzip`); my $image_output = "/tmp/grads_www/" . $function_name . "_" . time + . "_" . int(rand 1000); my $convert_to_gif_string = "/usr/homes/joew/ImageMagick-5.1.1/convert -rotate 90 $image_outpu +t.ps $image_output.gif"; $output .= "SHELL >> $convert_to_gif_string\n"; $output .= `$convert_to_gif_string`; if (open IMAGE_FILE, $image_output . ".gif") { print $query->header('image/gif'); while (<IMAGE_FILE>) { print; } } else { print $query->header('text/plain'); print $output; print "\nfailed on open: $image_output.gif\n"; }
Output when run from shell is the contents of the GIF file. Output when run as CGI is: SHELL >> /usr/homes/joew/ImageMagick-5.1.1/convert -rotate 90 /tmp/grads_www/meteo_955384773_91.ps /tmp/grads_www/meteo_955384773_91.gif failed on open: /tmp/grads_www/meteo_955384773_91.gif Anyone know what is going on? Or even any ideas on a work-around, or further tests to run? I'm completely stumped. FYI, if you can't tell, I'm using CGI.pm to handle CGI stuff. Thanks very much, -Joe

In reply to mysterious backticks/system() behavior in CGI script by jwielgos

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.