Zhao Yi
Zhao Yi

Reputation: 2465

Unable to write file with Apache CGI scripts

I want to add some logs to my CGI scripts with Perl code like this:

open(LOG, ">/path/to/my.log") or die;
print LOG "Some content...\n";
close(LOG);

However, logs are never written to my log file, while the scripts are still correctly handling requests.

I'm not very familiar with Apache, CGI and Perl, so gurus please shine a light.

Upvotes: 0

Views: 2620

Answers (2)

Zhao Yi
Zhao Yi

Reputation: 2465

The problem has been solved: changes to my Perl script take effect only after restarting Apache. Not sure why it behaves like this because I am thinking Perl is an interpreted language and it can be modified on the fly...

Upvotes: 0

Alan Haggai Alavi
Alan Haggai Alavi

Reputation: 74222

It is probably a permission problem. The script's runner (probably user: apache, httpd or nobody) has no permission to write to the file. However, to be sure, you need to check what $! contains. Also try checking Apache's ErrorLog file when the script is run.

I would rewrite your code as:

use CGI::Carp qw( croak );

open my $log, '>', '/path/to/my.log' or croak "Error opening file: $!";
print $log "Some content...\n";
close $log;

Upvotes: 1

Related Questions