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

I'm trying to create a back up file using CRON, creating a file using a timestamp. I don't have access to error logs and the script crashes out despite using Carp. I have whittled the ccode down to the following:
#!/usr/bin/perl -w use strict; use CGI::Carp qw(fatalsToBrowser warningsToBrowser); use CGI ':standard'; my $time = time; my $timepage = "/path/data/back_up/edit/$time.txt"; open FH, "> $timepage" or die "Can't open $timepage: $!"; close FH; print "Content-type: text/html\n\n"; print "$timepage";
Why doesn't the server like this?

Replies are listed 'Best First'.
Re: Why does this crash? (Error 500)
by tachyon (Chancellor) on Jan 02, 2004 at 10:05 UTC

    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

      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

Re: Why does this crash? (Error 500)
by jonnyfolk (Vicar) on Jan 02, 2004 at 10:04 UTC
    Did you set the permissions to create the file? Do the directories exist?
Re: Why does this crash? (Error 500)
by TomDLux (Vicar) on Jan 02, 2004 at 20:49 UTC

    If you are running a script as a cron job, why are you including the CGI module?

    Try reading man 5 cron.

    Include a line in cron:

    MAILTO=you@yourdomain,com

    and cron will mail you all error messages, printouts, ,and other output from your scripts.

    --
    TTTATCGGTCGTTATATAGATGTTTGCA

Re: Why does this crash? (Error 500)
by hawtin (Prior) on Jan 02, 2004 at 09:36 UTC

    I don't know why your program crashes but it is probably a good idea to redirect STDOUT and STDERR. Your last two lines are "really" doing:

    print STDOUT "Content-type: text/html\n\n"; print STDOUT "$timepage";

    So you may find the following snippet (mostly from the Camel) usefull

    open SAVEOUT, ">&STDOUT"; open STDOUT, ">/tmp/stdout.save"; . . print STDOUT "Content-type: text/html\n\n"; print STDOUT "$timepage"; . . open STDOUT,">&SAVEOUT";

    That's from memory so I wouldn't trust it

    Update:You should of course do the same for STDERR

Re: Why does this crash? (Error 500)
by Anonymous Monk on Jan 02, 2004 at 19:47 UTC
    Why doesn't the server like this?
    What does the server say (when the server doesn't like something, it tells you -- see the logs)?