I'm having issues getting a cron tab to run a java file every 5 minutes.

I have tried to create an executable script to see if that is what's needed:

My script and java file are both stored in /usr/local/bin and this is what I have in my crontab:

*/5 * * * * /usr/local/bin/java-crontab.sh

This is what I have in my script

export JAVA_HOME
export PATH
java -jar /usr/local/bin/javatest.jar

Sorry update - my JAVA_HOME file is not stored in usr/java but in usr/share/doc/openjdk-6-jre-headless. However when I enter that path in the executable script file I don't have permission to do so.

Brand new to creating VM's so apologies for my utter confusion here!

  • Welcome to Unix Stackexchange! You can take the tour first and the learn How to Ask a good question. That makes it easier for us to help you. – andcoz Feb 6 '17 at 13:40
  • @hannah-s check the mailbox of the user that owns the crontab for error log emails, – andcoz Feb 6 '17 at 13:43
  • Do you think you have to define JAVA_HOME each time you run a java app? You don't have to. Just use the absolute path to java: /usr/share/doc/openjdk-6-jre-headless/java -jar ... – ka3ak Feb 6 '17 at 16:06
  • Ahhh i didn't know - it still isn't working I'm afraid unless i still have a typo, iv changed the script file to: #!/bin/bash export PATH=/usr/share/doc/openjdk-6-jre-headless:$PATH java -jar /usr/local/bin/javatest.jar but it isn't running :( – Hannah.S Feb 6 '17 at 16:25

your java-crontab.sh has a typo. JAVA_HOME should be an absolute path.

Simplifying, you could go with

export PATH=/usr/java/jdk1.6.0_40:$PATH
java -jar /usr/local/bin/javatest.jar
| improve this answer | |
  • I've just realised that JAVA_HOME is set in /usr/lib/jvm/java-6-sun. If I give rwx permissions and place that in my script should that theoretically work? – Hannah.S Feb 6 '17 at 14:10
  • In theory. Don't forget to replace /usr/java/jdk1.6.0_40 with /usr/lib/jvm/java-6-sun. Try running the script manually before configuring your crontab. If it fails, tell us what's going on, ... – SYN Feb 7 '17 at 5:51
  • Hello, so I ran the script using the bash command and I got an error saying 'exception in thread "main" java.lang.reflect.inovocationtargetexception etc etc – Hannah.S Feb 7 '17 at 17:56
  • without the full trace and some visual on your sources, there's no way of telling what happened. Regardless, my first guess would be you're running an old version of Java: are you sure this jar is supposed to work with 1.6? – SYN Feb 7 '17 at 18:08
  • The sys log file just says 'NO MTA installed, discarding output' I did install postfix but the crontab still was not running the jar file so I returned to my earlier snapshot which was before I installed postfix. I also created a new logfile in hope the crontab would report the errors into that but the logfile remained blank – Hannah.S Feb 8 '17 at 17:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.