Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: System commands using CGI

by blue_cowdawg (Monsignor)
on Dec 03, 2012 at 17:06 UTC ( #1006914=note: print w/replies, xml ) Need Help??

in reply to System commands using CGI

      Hi, I am trying to execute system commands from CGI but it doesnt work.If I execute the same code from command line, the code works fine.

This is a very common "gotcha" that it would seem all Perl programmers and Perl programmer wannabes go through at some point in their Perl travels.

Stepping back from your original complaint for just a second let me share with you my typical approach for troubleshooting a CGI problem.

  1. First off understand exactly how your web server is set up. What userid is your CGI environment running as being one of the first things I want to know about. Don't assume anything, that's bad for you, stunts your growth, curls your spine and causes you to forget to use strict; in your coding. :-) A handy piece of code that I use is as follows:
    #!/usr/bin/perl -w #----------------------------------- # Script to dump my environment to a browser from CGI. # CAVEAT: Never, ever by all that is holy leave this script in a # production environment use strict; use CGI qw/ :all /; my $cgi = new CGI; print $cgi->headers,$cgi->start_html; # start the show; print hr,b("ENV Dump"); print ul( map { li($ENV{$_} } keys %ENV ); print hr,b("CGI Param Dump"); print ul( map { li($cgi->param($_)) } $cgi->param ); print $cgi->end_html; exit(0);
    This will give you an idea what you are dealing with and will be a big help in troubleshooting.
  2. Find out where your server logs are and start watching them.
  3. Embed some debugging statements into your code.
  4. Run your code and watch all the fun

Given your original complaint I'd look at system permissions for both running the command in the first place (which is why your code will behave differently at the command line than when run from CGI). Typically CGI is run as a non-elevated user and on some *nix systems it is run as "nobody" or "nofiles" which will eliminate your ability to create/modify files.

Hope this helps you along your way.

Peter L. Berghold -- Unix Professional
Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg

Replies are listed 'Best First'.
Re^2: System commands using CGI
by Anonymous Monk on Jul 11, 2013 at 15:09 UTC
    Helpful, but some syntax errors:


    should be:


    missing closing ")":

    map { li($ENV{$_} } keys %ENV

    should be:

    map { li($ENV{$_}) } keys %ENV

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1006914]
[james28909]: some kind of logic behind it
[james28909]: and that is also anothe rpoint i made, i think it has to do with perception of the world around you. most people think of evolution on a human scale. why could life evolve on this planet? because this planet evolved in this solar system. and so on.
[holli]: here's something for you to watch, James. I think you will like it
[erix]: for the record: I have not downvoted anyone on that subthread that was my fault
[james28909]: there are all kinds of things that had to happen to let life come to be. but at the same time, life may not be the end goal IF there is any kind of end goal lol
[james28909]: well who is the person who gets to decide which behaviour is worthy of a downvote? a person with their own beliefs? xD
[erix]: teleology -- I've never understood why that was thunk up
[erix]: ( and when teleology was brought up, during my biology-study., I couldn't get an answer either )
[1nickt]: FTR I agree with you about "design." Just not sure about the trajectory of evolution. THere are a lot of dead-ends in the evolutionary paths. I suspect homo sapiens is just one of them.
[james28909]: thanks for the vid holli, i do like it. thanks for sharing :)

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (15)
As of 2017-12-15 14:37 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (433 votes). Check out past polls.