bashcronpipegoaccess

Cron Pipe zcat on Bash Script not working


I created a script based on the tutorial on this page: https://johnveldboom.com/posts/goaccess-automated-reports-last-30-days-via-cron/. When the script is run from the terminal it works perfectly. The problem is when cron runs the script, pipe does not seem to work.

I Googled for possible solutions. I tried adding "-c" to the bash on cron but that did not help.

This is the Script

 #!/bin/bash
 # filename: goaccess.sh (with +x permission)
 HOST="myHost"
 GOACCESSREPORT_DIR=/home/user/goaccess_reports/
 DATE=$(date +'%Y.%m')
 /bin/zcat `find /var/log/nginx/ -name "myhost_access.log.*.gz" -mtime -20` | goaccess > $GOACCESSREPORT_DIR/$HOST-monthly-$DATE.html
 echo "My Host GoAccess Report" | sudo mail -s "My Host GoAccess Report" email@test.com -A $GOACCESSREPORT_DIR/$HOST-monthly-$DATE.html

This is my cron

 00 22 * * 5 /bin/bash -c /home/user/goaccess.sh

The output file information is below, which lets me to think that piping is not working:

GoAccess - 1.2 Usage: goaccess [filename] [ options ... ] [-c][-M][-H][-q][-d][...] The following options can also be supplied to the command: Log & Date Format Options --date-format= - Specify log date format. e.g., %d/%b/%Y --log-format= - Specify log format. Inner quotes need to be escaped, or use single quotes. --time-format= - Specify log time format. e.g., %H:%M:%S User Interface Options -c --config-dialog - Prompt log/date/time configuration window. -i --hl-header - Color highlight active panel. -m --with-mouse - Enable mouse support on main dashboard. --color= - Specify custom colors. See manpage for more details and options. --color-scheme=<1|2|3> - Schemes: 1 => Grey, 2 => Green, 3 => Monokai. --html-custom-css= - Specify a custom CSS file in the HTML report. --html-custom-js= - Specify a custom JS file in the HTML report. --html-prefs= - Set default HTML report preferences. --html-report-title=

Thank you in advance.


Solution

  • I had the same problem and found the answer here.

    You need to use a dash after the goaccess command to tell it you are piping the logs in:

    /bin/zcat `find /var/log/nginx/ -name "myhost_access.log.*.gz" -mtime -20` | goaccess - > $GOACCESSREPORT_DIR/$HOST-monthly-$DATE.html