phpawstats

Trigger awstats update from PHP


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.


Solution

  • 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);