0

I have a script named run_logs.sh with the contents:

#!/bin/bash
source /root/.profile
zcat /var/log/apache2/access.log.*.gz | /usr/bin/goaccess - access.log

when I run that script as root (both with and without a 'bash' prefix', on the command line the goaccess software processes both access.log and all of the gzipped logs. But when cron runs it, it only processes the access.log. What can I do to correct this?

My root crontab looks like:

0 * * * * /root/run_logs.sh
kevincw01
  • 21
  • 2
  • 1
    What happens if you just put the script directly in crontab? – Paul Jun 10 '23 at 02:41
  • Add full path to `goaccess` and set environment variables. – Romeo Ninov Jun 10 '23 at 04:16
  • updated to reflect suggestions from Zareh which are technically correct but didn't resolve it. – kevincw01 Jun 10 '23 at 15:02
  • @Paul No difference. – kevincw01 Jun 10 '23 at 15:02
  • @RomeoNinov I updated to reflect the full path to access and it did not make a difference. What environment variables are you suggesting I should set to resolve this? – kevincw01 Jun 10 '23 at 15:04
  • @kevincw01m the variables you have in your shell environment. Try to add on the begin of script (after shebang) `source /path/to/your/home/directory/.bash_profile` and check again – Romeo Ninov Jun 10 '23 at 15:21
  • I do not have .bash_profile but I did find .profile and I added that and there was no change. I also tried adding source .bashrc there was also no change – kevincw01 Jun 10 '23 at 16:03
  • As a note, the goaccess parameters above are over simplified so as to not add unnecessary complexity. The key is to use the hyphen when used as a cron job. – kevincw01 Jun 10 '23 at 16:25

2 Answers2

2

The solution was found here which essentially says that goaccess has a specific requirement to add a hyphen in the parameters to process multiple files. However for some reason, it works without this when run on the command line and not when run in cron. I've updated the correct command in the OP to reflect the additional goaccess parameter.

kevincw01
  • 21
  • 2
0

Change your script as below.

Add #!/bin/bash, at the beginning of a script, indicating that the script should be executed using the Bash shell.

#!/bin/bash 
 zcat /var/log/apache2/access.log.*.gz | goaccess access.log

Make the script executable.

chmod +x /root/run_logs.sh

Update your crontab entry to point directly to the script.

0 * * * * root /root/run_logs.sh
Zareh Kasparian
  • 753
  • 5
  • 20