3

I'm trying to get this simple bash script to work within a cronjob. It's used to generate static nginx webserver statistic pages using GoAccess.

I tried everything I know to resolve this issue, it just won't work as a cronjob. It's executed perfectly in console. It's even working as cronjob when I remove the call to goaccess (e.g. put an echo there to see if the call is built correctly).

Any help on this? The script runs for every file but the resulting files only contain the "how to use" instructions from goaccess which appear when you call it without arguments.

System is a vServer running Debian 6.0. I use GoAccess 0.5 and nginx 1.2.1

Here's the script:

#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/home/

_logdir="/srv/www/logs"
_logfiles="${_logdir}/access_*.log"

for logfile in $_logfiles; do
        _curfilename="${logfile##*/}"
        # All these commands work flawless
        #echo "$logfile" >> /home/debug.log
        #echo "$_curfilename" >> /home/debug.log
        #echo "goaccess -a -f $logfile > /srv/www/stats/${_curfilename}.html" >> /home/debug.log;

        # This one fails
        goaccess -a -f "${logfile}" > "/srv/www/stats/${_curfilename}.html";
done

Here's the crontab line for once an hour (i used * * * * * for debugging):

0 * * * * /bin/bash /home/run_webstats_update.sh

Error output in cron.log:

/USR/SBIN/CRON[26099]: (root) CMD (/bin/bash /home/update_webstats.sh)
/USR/SBIN/CRON[26098]: (CRON) error (grandchild #26099 failed with exit status 1)
5
  • 2
    You might want to hardcode the relative path for "goaccess", example: /usr/local/bin/goaccess. Cron may not be familiar with the same paths that you use.
    – Tim
    Jun 22, 2012 at 19:10
  • I assume you have tried specifying SHELL=/bin/bash in crontab to totally eliminate dash. Does appending make any difference? Now you use >, try >>. Jun 22, 2012 at 20:05
  • Redirect standard error somewhere by adding something like 2>/some/file to the command line. If goaccess is printing an error, it should end up in that file. Jun 22, 2012 at 21:55
  • 1
    Check your local mail for the error messages. Cron sends error output from jobs in a local mail. (This implies that you've set up local mail properly; if you haven't, do this then try again.) Jun 23, 2012 at 0:18
  • Thanks for the hints. I'll try your tips as well, but at the moment I got it working using Josh's answer.
    – Manuel
    Jun 23, 2012 at 10:31

2 Answers 2

1

This should do it. Just make sure you got the right paths. BTW, GoAccess is nifty.

#!/bin/bash
PATH=$PATH:/home/

_logdir="/srv/www/logs"
_logfiles="${_logdir}/access_*.log"

for logfile in $_logfiles; do
   _curfilename="${logfile##*/}"
   cat "${logfile}" | goaccess -a > "/srv/www/stats/${_curfilename}.html"
done

You could even parse compressed data files:

zcat -f access.log* | goaccess -a > "/srv/www/stats/${_curfilename}.html"

3
  • I've modified the PATH variable, by setting the variable like you did any exisitng directory was removed. By appending a directory to the path you increase the change that goaccess is found. It's still better to use the full path to goaccess (or any other tool) in a script though.
    – Bram
    Jun 23, 2012 at 9:34
  • Using the "cat" command did the trick. Thanks a lot!
    – Manuel
    Jun 23, 2012 at 10:29
  • Just stumbled upon this problem. Not being able to use the -f flag with GoAccess from within cron just cost me an hour of my life.
    – Jeff
    Sep 2, 2013 at 18:21
1

What is goaccess? it is not a standard command nor a function you have already defined, you need to declare it in the beginning or source the file that has the function

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.