LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-25-2010, 01:08 PM   #1
Nintendo
LQ Newbie
 
Registered: May 2010
Posts: 9

Rep: Reputation: 0
Question Simple perl log code not working.


I just upgraded servers, and I'm now on 'perl, v5.8.8 built for i386-linux-thread-multi' and a simple log script doesn't work. I got it changed to...

Code:
#!/usr/bin/perl

use strict;
use warnings;
use POSIX qw(strftime);
use CGI;
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);

my $cgi             = CGI->new;
my $database        = "/full_path/file.shtml";
my $shortdate       = strftime("%m/%d/%y %H:%M:%S %Z", localtime);
my $remote_addr     = $ENV{'REMOTE_ADDR'} || 'N/A';
my $script_uri      = $ENV{'SCRIPT_URI'} || 'N/A';
my $http_user_agent = $ENV{'HTTP_USER_AGENT'} || 'N/A';
my $http_referer    = $ENV{'HTTP_REFERER'} || 'N/A';

print $cgi->header, $cgi->start_html;
warningsToBrowser(1);
print $cgi->p("$remote_addr - $shortdate - $script_uri - $http_user_agent<!-- - $http_referer-->");

open my $dbhandle, '>>', $database
   or die "failed to open '$database' for appending: $!";

print $dbhandle "$remote_addr - $shortdate - $script_uri - $http_user_agent<!-- - $http_referer--><BR>\n";
close $dbhandle;
where it only recognizes the time. It doesn't recognize any of the $ENV stuff. It spits out

N/A - 05/25/10 10:53:14 PDT - N/A - N/A - N/A

in to the log file. It only does logging when accessed via SSH, and does nothing when called from the script. (Script spits out the ISE message.)

The original script that worked fine before the server upgrade...

Code:
#!/usr/bin/perl
use CGI ':standard';
use warnings;

$database = "/full_path/file.shtml";

$shortdate = `date +"%D %T %Z"`; 
chop ($shortdate);
    open (DATABASE,">>$database");
        print DATABASE "$ENV{'REMOTE_ADDR'} - $shortdate - $ENV{'SCRIPT_URI'} - $ENV{'HTTP_USER_AGENT'}<!-- - $ENV{'HTTP_REFERER'}--><BR>\n";
    close(DATABASE);

{
   print "Content-Type: text/html\n\n";
print <<EOM;
<PRE>
</PRE>
EOM
   exit;
}
Does any one know how to get one of those versions working?
 
Old 05-25-2010, 01:49 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Nintendo View Post
...
It doesn't recognize any of the $ENV stuff. It spits out
...
Environment variables are inherited from the calling process, so dump the environment (something like 'env | sort') just before calling the script to see what environment the script is supposed to inherit.

If you do not have the environment variables you expect to have, the script has nothing to do with it.
 
Old 05-25-2010, 03:18 PM   #3
Nintendo
LQ Newbie
 
Registered: May 2010
Posts: 9

Original Poster
Rep: Reputation: 0
Turns out it's permission issues.

I started getting a *useful* 'ISE' message...

97.125.24.27 - 05/25/10 11:18:46 PDT - N/A - Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.3) Gecko/20100401 Ant.com Toolbar 2.0.1 Firefox/3.6.3

Software error:

failed to open '/var/www/vhosts/ANOTHERDOMAIN.com/httpdocs/directory/file.shtml' for appending: Permission denied at thisfile.cgi line 21.

which is

open my $dbhandle, '>>', $database
or die "failed to open '$database' for appending: $!";

so I then moved the log file to the same domain, chmod it 777, changed the path in the script...and the original version of the script worked just fine.

Is there any way to make the script be allowed to post data to a file in another domain on the same server? The file on the other domain and the directory that it's in is both chmod 777.
 
Old 05-25-2010, 09:45 PM   #4
Nintendo
LQ Newbie
 
Registered: May 2010
Posts: 9

Original Poster
Rep: Reputation: 0
chmod'ing all folders back to the domain 777 does it. For example, two folders and a file...

httpdocs/folder/file.shtml

and it then works.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
simple perl script not working vinaytp Linux - Newbie 1 06-19-2009 01:13 AM
perl: simple one line replacement, simple error message! pwd_pwd_omg_pwd Programming 8 11-19-2008 12:53 PM
Error in Perl Code : Bad switch statement(Problem in code block)? near ## line # suyog255 Programming 4 02-20-2008 05:35 PM
Hiding code in PERL, perl gui question randomx Programming 1 06-26-2004 03:22 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration