1

This question is related to Gentoo Linux.

I have cron job that runs every morning at 10AM. The cron job calls a bash script which in turn calls a groovy script that queries the database for a column of data and emails the result to a recipient list.

Every day the script executes, it sends out the exact same data. When I run the script manually it, reports (and emails) the correct data. I'm at a complete loss of how to further troubleshoot this. What gives?

Here's my cronjob script:

#!/bin/bash

OUTPUT=""
TEMPFILE=/tmp/dsr.output
MAILTO="[email protected]"
INSTALL_DIR=/usr/local/clearleap/scripts/daily-storage-report

export CLASSPATH=$CLASSPATH:$INSTALL_DIR/lib/postgresql-8.3-603.jdbc4.jar:$INSTALL_DIR

OUTPUT=$(/opt/groovy/bin/groovy $INSTALL_DIR/DailyStorageReport.groovy)
echo "$OUTPUT" > $TEMPFILE
echo $OUTPUT

echo $OUTPUT | /bin/mailx  -s "Daily Storage Report" $MAILTO

Here is the cronjob itself, which lives in /etc/cron.d/dsr_cron:

0 10 * * * root  /usr/local/clearleap/scripts/daily-storage-report/dsr.sh 

Please let me know if you'd like to see the code from the groovy script. I have no idea why it appears to be caching!

Any help is greatly appreciated!

6
  • appart from the email, is the $OUTPUT correct ? Have you checked the content of the TEMPFILE with and without the cronjob ?
    – Rosco
    Jan 30, 2013 at 16:42
  • Does the account you're running it under have access to modify /tmp/dsr.output?
    – ceejayoz
    Jan 30, 2013 at 17:00
  • The $TEMPFILE contains the incorrect data, the email that gets sent has the incorrect data. BUT, when i run the script manually it spits out (and emails) the correct data!
    – slappyjam
    Jan 30, 2013 at 17:02
  • what do you get when you run echo $CLASSPATH from the command line? It is possible that the environment variables cron has could be different from what is available when you run it manually.
    – Daniel t.
    Jan 30, 2013 at 17:07
  • 3
    TEMPFILE=/tmp/dsr.output - I strongly suggest you consider using mktemp or something that will create a randomly name temporary file. Imagine what would happen if /tmp/dsr.output didn't exist and some unprivileged user or serves made /tmp/dsr.output a symlink to /etc/passwd? Secure temp file usage - codeproject.com/Articles/15956/…
    – Zoredache
    Jan 30, 2013 at 19:34

2 Answers 2

0

I think your problem lies here:

echo "$OUTPUT" > $TEMPFILE

It should be

echo "$OUTPUT" >| $TEMPFILE

Since > if the file exists won't overwrite it unless noclobber is set.

0

Your problem seems to be that your Groovy doesn't run from cron at all, so it just keep sending the same email.

What you can do, add to top of your script:

source /etc/bashrc

The full line could look like this:

30 * * * * source /etc/bashrc; /opt/groovy/bin/groovy /path/to/groovy/script.groovy >>/var/log/MyGroovyScript-stdout.log 2>/var/log/MyGroovyScript-error.log

Or whatever is appropriate for your environment.

In that file, /etc/bashrc, you need to add the following:

export JAVA_HOME=/usr/java/latest

Once you have JAVA_HOME in your environment variables it will work. Cron does not use /etc/bashrc on it's own so that your JAVA variable might be missing completely.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .