Score:0

crontab shell script execution not as expected

cn flag

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
Paul avatar
cn flag
What happens if you just put the script directly in crontab?
Romeo Ninov avatar
in flag
Add full path to `goaccess` and set environment variables.
kevincw01 avatar
cn flag
updated to reflect suggestions from Zareh which are technically correct but didn't resolve it.
kevincw01 avatar
cn flag
@Paul No difference.
kevincw01 avatar
cn flag
@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?
Romeo Ninov avatar
in flag
@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
kevincw01 avatar
cn flag
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 avatar
cn flag
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.
Score:2
cn flag

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.

Score:0
us flag

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
kevincw01 avatar
cn flag
I made these changes (except chmod because script was already executable) and it did not make a difference.
Romeo Ninov avatar
in flag
Based on the OP question he put `cron` record in own file and format you suggest is appropriate for `cron.daily`!
Zareh Kasparian avatar
us flag
@kevincw01 add the full path of zcat command to the script. The full path could be found with "which zcat" command
kevincw01 avatar
cn flag
@zareh this was not the solution. See the solution below.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.