0

I am trying to run a .class file from a bash script every 1 minute using the crontab. I get the error Could not find or load main class Cron_Read_Send_CapacityData_To_Graphite. I have already set environment variable on my machine.

If i run the bash script from the terminal it works fine.Note the bash script and the java class file are in the same folder

Script:

$ cat Run_Cron_Read_Send_CapacityData_To_Graphite.sh
#!/bin/bash

java Cron_Read_Send_CapacityData_To_Graphite >> /home/marshell/graphite_cronjobs/Cron_Read_Send_CapacityData_To_Graphite.log

Cron entry:

$ crontab -e

*/1 * * * * /home/marshell/graphite_cronjobs/Run_Cron_Read_Send_CapacityData_To_Graphite.sh >> /home/marshell/graphite_cronjobs/debug_cronjob.log 2>&1

Error:

Error: Could not find or load main class Cron_Read_Send_CapacityData_To_Graphite
6
  • 1
    Environment variables you set in your shell init files (or in your interactive shell) may not be in effect for a shell run from cron (depending on how you set it). Try setting class path/etc. in the script explicitly. Commented Sep 17, 2014 at 15:28
  • Hi i set it in /etc/environment this is the line i added JAVA_HOME="/usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java" Commented Sep 17, 2014 at 15:30
  • Does it work if you run the script without cron? Commented Sep 17, 2014 at 15:32
  • I believe /etc/environment is a pam thing and cron is likely not dealing with that either. Commented Sep 17, 2014 at 15:32
  • Yes it works if i don't use cron job. I tried running the .sh file from terminal it works fine Commented Sep 17, 2014 at 15:33

1 Answer 1

1

You error message indicates that your classpath isn't set correctly for the script to find Cron_Read_Send_CapacityData_To_Graphite.class, I would use something like -

CP=/home/marshell/graphite_cronjobs # The classpath to use
LF=$CP/Cron_Read_Send_CapacityData_To_Graphite.log # the log file
java -cp $CP Cron_Read_Send_CapacityData_To_Graphite >> $LF
Sign up to request clarification or add additional context in comments.

1 Comment

will this crontab entry start on system reboot ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.