in reply to Why does this crash? (Error 500)

CGI::Carp fatalsToBrowser does not work all the time as you have discovered. You will be dieing in the open and CGI::Carp does not catch this. Uncomment the typo and it will catch that, but not the die. It is a bug in CGI::Carp which is why I don't bother to use it. See CGI Help Guide for all sorts of tips (including a way to get almost all your errors onto the screen). Anyway this mod will tell you that the path does not exist or you don't have the required perms and you are dieing on the open.....

#!/usr/bin/perl -w use strict; use CGI::Carp qw(fatalsToBrowser); use CGI ':standard'; print "Content-type: text/html\n\n"; my $time = time; my $timepage = "/path/data/back_up/edit/$time.txt"; #typo open FH, "> $timepage" or die_nice( "Can't open $timepage: $!" ); close FH; print "$timepage"; sub die_nice { my ( $err ) = @_; print "Content-Type: text/html\n\n$err\n"; warn "ERR - $err\n"; exit 1; }

I recommend using a die_nice routine in all CGI rather than a naked die. Note I always print a header in this so you won't 500. Also note that I get the header out ASAP so you won't 500. The only really good reason not to put the header out in the first few lines is that you may want to send a cookie or image or other header later.

The other possibilities include no Perl, CGI or CGI::Carp are not installed (so you have compile errors) or no execute perms ie you forgot to chmod 755 your script. But it smells like permissions or path.

PS When this sort of thing occurs the quickest way to the answer is to distill ie try:

#!/usr/bin/perl print "Content-Type: text/html\n\nWe have ignition!" #!/usr/bin/perl use CGI ':standard'; print header(), 'Engage CGI'; #!/usr/bin/perl use CGI ':standard'; use CGI::Carp qw(fatalsToBrowser); print header(), 'Enterprise You are clear to go with Carp'; print "Roger Houston testing Carp with typo now etc

cheers

tachyon

Replies are listed 'Best First'.
Re: Re: Why does this crash? (Error 500)
by Anonymous Monk on Jan 02, 2004 at 10:36 UTC
    Thanks very much, Tachyon - I used your die_nice() routine and found that I had permissions denied. I have been through the path and checked that all folders are 755 so I'm not sure what to do now (Guess I'll have to email server admin!). I shall use die_nice() for evermore...

      You need 777 if you just want it to work!

      4 Read 2 Write 1 Execute ie 5 = R+X (4+1) 6 = R+W (4+2) 7 = R+W+X (4+2+1) 7 5 5 owner group everyone-else

      Your CGI will run as the user apache or nobody. So for your script to write to a dir user apache/nobody needs to be able to WRITE to the dir (it will probably be falling into the everyone-else user cat). Assuming you own it (the 7) you either need to change the ownership and or the perms. Something like this will work: chmod -R 777 /path/to/dir/ but is pretty crude. You probably want something like chown you:nobody /path/to/dir && chmod 775 /path/to/dir which will make you the owner, nobody (or apache if req) the group and then give the group write perms. You may want 770 if the data is senstive.

      Oh, and the usual text message you have in a die_nice goes like ;-)

      Sorry the system is currently unable to fulfil you request due to: 1) Routine Maintenance 2) Unusually high load 3) Transient network hiccups and miscalaneous flatulence 4) Being on strike cause of the 24/7 working hours, low pay and total lack of appreciation from my programmers. .....

      cheers

      tachyon

        You should be aware that using mode 777 means that anybody on the system can read and write that file or directory. If you're on a shared system, anybody else on the system can edit or delete files in a mode 777 directory. Even if you have a system to yourself, giving permissions to everybody to write someplace can make a minor security problem (like disk access as user nobody) into a big one.

        If you really don't care about the files or directories with mode 777 being changed by random users, those permissions are fine. Sometimes that's true, as in /tmp. Mostly, though, it's not true, and you should find a more restrictive set of permissions that allows your script to write to that directory, without allowing the rest of the world as well.

        Ah, 777 did it!! Thanks tachyon, and the text message is copied too! :)