PIWIK cron job

apps

#1

I’ve been batteling with getting the cron job configuration right for the Piwik archiving. I would like to know if anybody got it done with the DH cron jobs panel? If please be so kind to post an example of the command line.

So far I got it like this, but it does not seem to work:

/usr/local/php5/bin/php /home/myuser/mypage.com/statistics/misc/cron/archive.sh

myuser = the shell user with access to data
archive.sh = the file that should be executed by the cron

Any help will be highly apprechiated!


#2

The first thing I see (and I am not familiar with the operation of PIWIK, or the script you are trying to run), is that you are running a shell script (to be run via the shell, as indicated by the “.sh” filetype in "archive.sh:slight_smile: with PHP. That is not a PHP script. :wink:

Assuming your “archive.sh” file is set to be executable, you don’t need the “/usr/local/php5/bin/php” part at all.

Does just typing: /home/myuser/mypage.com/statistics/misc/cron/archive.sh at the shell command line and hitting the enter key produce the desired results? If so, that is all you need in the cronjob (if entering it via the account control panel) :wink:

If you aren’t working in the shell, but are instead entering this in the panel, try just that line in the entry box for the command to be run.


#3

I’m not familiar with shell so I tried it in the account control panel like you suggested:

/home/myuser/mypage.com/statistics/misc/cron/archive.sh

The result is now (mail by Cron Daemon):

sh: /home/myuser/mypage.com/statistics/misc/cron/archive.sh: Permission denied


#4

Not knowing the contents of the script, it could be a permissions issue. It should be set to 755, or rwxr-xr-x. Try WebFTP from the panel here for Manage Domains and see if has a Permissions (or CHMOD) option.


#5

What Scott said! To clarify, that “archive.sh” file should be set to those permissions.


#6

I’ve set the permissions for “archive.sh” to 755 and the cron job executed with the following error message:

php binary not found. Make sure php5 or php exists in PATH.

Below I have attached the content of the file “archive.sh”:

[i]#!/bin/bash -e

Description

This cron script will automatically run Piwik archiving every hour.

The script will also run scheduled tasks configured within piwik using

the event hook ‘TaskScheduler.getScheduledTasks’

It automatically fetches the Super User token_auth

and triggers the archiving for all websites for all periods.

This ensures that all reports are pre-computed and Piwik renders very fast.

Documentation

Please check the documentation on http://piwik.org/docs/setup-auto-archiving/

How to setup the crontab job?

Add the following lines in your crontab file, eg. /etc/cron.d/piwik-archive

#---------------START CRON TAB–
#MAILTO=“youremail@example.com”
#5 * * * * www-data /path/to/piwik/misc/cron/archive.sh > /dev/null
#-----------------END CRON TAB–

When an error occurs (eg. php memory error, timeout) the error messages

will be sent to youremail@example.com.

Optimization for high traffic websites

You may want to override the following settings in config/config.ini.php:

See documentation of the fields in your piwik/config/config.ini.php

[General]

time_before_archive_considered_outdated = 3600

enable_browser_archiving_triggering = false

#===========================================================================

for TEST_PHP_BIN in php5 php php-cli php-cgi; do
if which $TEST_PHP_BIN >/dev/null 2>/dev/null; then
PHP_BIN=which $TEST_PHP_BIN
break
fi
done
if test -z $PHP_BIN; then
echo "php binary not found. Make sure php5 or php exists in PATH."
exit 1
fi

act_path() {
local pathname="$1"
readlink -f “$pathname” 2>/dev/null ||
realpath “$pathname” 2>/dev/null ||
type -P “$pathname” 2>/dev/null
}

ARCHIVE=act_path ${0}
PIWIK_CRON_FOLDER=dirname ${ARCHIVE}
PIWIK_PATH="$PIWIK_CRON_FOLDER"/…/…/index.php
PIWIK_CONFIG="$PIWIK_CRON_FOLDER"/…/…/config/config.ini.php

PIWIK_SUPERUSER=sed '/^\[superuser\]/,$!d;/^login[ \t]*=[ \t]*"*/!d;s///;s/"*[ \t]*$//;q' $PIWIK_CONFIG
PIWIK_SUPERUSER_MD5_PASSWORD=sed '/^\[superuser\]/,$!d;/^password[ \t]*=[ \t]*"*/!d;s///;s/"*[ \t]*$//;q' $PIWIK_CONFIG

CMD_TOKEN_AUTH="$PHP_BIN -q $PIWIK_PATH – module=API&method=UsersManager.getTokenAuth&userLogin=$PIWIK_SUPERUSER&md5Password=$PIWIK_SUPERUSER_MD5_PASSWORD&format=php&serialize=0"
TOKEN_AUTH=$CMD_TOKEN_AUTH

CMD_GET_ID_SITES="$PHP_BIN -q $PIWIK_PATH – module=API&method=SitesManager.getAllSitesId&token_auth=$TOKEN_AUTH&format=csv&convertToUnicode=0"
ID_SITES=$CMD_GET_ID_SITES
echo "Starting Piwik reports archiving…"
echo ""
for idsite in $ID_SITES; do
TEST_IS_NUMERIC=echo $idsite | egrep '^[0-9]+$'
if [ “$TEST_IS_NUMERIC” ]
then
for period in day week year; do
echo ""
echo “Archiving period = $period for idsite = $idsite…“
CMD=”$PHP_BIN -q $PIWIK_PATH – module=API&method=VisitsSummary.getVisits&idSite=$idsite&period=$period&date=last52&format=xml&token_auth=$TOKEN_AUTH”;
$CMD
done

echo ""
echo "Archiving for idsite = $idsite done!"

fi
done

echo “Reports archiving finished.”

echo "Starting Scheduled tasks…"
echo ““
CMD=”$PHP_BIN -q $PIWIK_PATH – module=API&method=CoreAdminHome.runScheduledTasks&format=csv&convertToUnicode=0&token_auth=$TOKEN_AUTH”;
$CMD
echo "Finished Scheduled tasks."
echo “”[/i]


#7

That’s weird, because a php binary is in the path somewhere (/usr/local/bin). I have only two ideas:

  1. Add this line after the big line of equal signs:
echo $PATH

and let us know what it shows.

  1. Edit your user in the DreamHost panel under Manage Users so it’s a Shell Account. Go ahead and set it to be a /bin/bash shell, but I don’t think that part matters. All of my accounts are shell accounts and they don’t have the PATH issue you’re having.

#8

Ok, I’ve checked my settings for the user and it was already set to “Shell Account” and also as “/bin/bash”…

After adding the line echo $PATH and executing the cron job I´m getting the following message:

/bin/bash: -
: invalid option

(@Scott - Thank you for looking at this issue!)


#9

That’s a classic symptom of a shell script saved with DOS line endings.


#10

Yeah, the file was corrupted. I tried dos2unix but that didn’t save it, so I re-uploaded a fresh one for him.


#11

Thank you very much to everyone who helped by solving this issue!
Special thanks to “sdayman”, without the dedication of people like you us newbies would be totally lost…

Everything works fine now…