1

I am trying to trigger an update of AWStats from inside a PHP script.

I currently use a cron job to trigger the update, and simply copied the command line into an exec function within the script.

if(exec("/path/to/awstats.pl -config=domain.com -update")) {
    echo 'Logs processed';
}

However, this returns a false positive. Although the "Logs processed" line is displayed, AWStats has not processed the stats information.

AWStats does work perfectly when visited directly, and when running the update via the cron job, it just isn't from this PHP script. I have checked the error logs, there is not a problem with my script or with AWStats timing out.

Am I missing something?

For the record, this script is designed to purge the old data, update a blacklist of referrers to block spam, and then recompile the stats data from the log files. Yes, I am aware of the performance issues of using the SkipReferrerBlackList directive.

Geoff Atkins
  • 1,693
  • 1
  • 17
  • 23
  • Does the cronjob run as the same user as the PHP script? – aross Feb 28 '17 at 10:40
  • @aross Ah, this is on a shared hosting server. I believe so, but I don't know for sure. – Geoff Atkins Feb 28 '17 at 10:43
  • Try to manually append something to the files the script writes to. From the PHP script, of course. It could just be a permission issue – aross Feb 28 '17 at 10:44
  • @aross I executed the PHP script as a cron job, and the result was the same. The script does append data to a file as well (which is its purpose) and that works fine. – Geoff Atkins Feb 28 '17 at 10:50

1 Answers1

2

It seems from your code that you think exec returns a boolean indicating success or failure. It doesn't, it just returns a string (the last line of output from the command). And strings (except "0" and an empty string) always evaluate to true.

To debug the problem you should print the output of the command:

exec("/path/to/awstats.pl -config=domain.com -update", $output);
echo join(PHP_EOL, $output);
aross
  • 3,325
  • 3
  • 34
  • 42
  • Thanks, @aross. Yes, I wasn't thinking properly and I did assume a boolean response. I also think the PHP script isn't authorised to run `.pl` file, and the output was an array. From this I've found a solution by creating a `.cgi` file which the script can run. – Geoff Atkins Feb 28 '17 at 11:19