I've tried everything I can think and searched high and low for solutions of but I can't get JAVA_HOME into cron to run anything java periodically.

In terminal

java -version

gives me everything you'd expect. 'which java' prints '/usr/bin/java'

I'm also running Java applications fine from bash scripts launched in terminal.

I want to run them periodically from cron but it is refusing to.

My crontab

* * * * * /bin/bash --version > /tmp/b.log
* * * * * /usr/bin/java -version > /tmp/j.log

Which fills b.log with text but j.log is empty.

I have also tried adding export JAVA_HOME=/usr/lib/jvm/java-6-sun to the top of scripts as well but no dice.

Any pointers?


The problem is that java -version does not seem to be writing to stdout

rhand@server:/tmp$ /usr/bin/java -version > t.log
java version "1.6.0_20"
OpenJDK Runtime Environment (IcedTea6 1.9.13) (6b20-1.9.13-0ubuntu1~10.04.1)
OpenJDK Client VM (build 19.0-b09, mixed mode, sharing)

The solution would be to use this command:

/usr/bin/java -version 2> /tmp/j.log
  • Actually, with five '*'s, the job in crontab should be run once each minute. – G. He May 15 '12 at 13:25
  • Sorry, already changed my answer as I realised I made a mistake. – Rhand May 15 '12 at 13:27
  • I forgot to include the solution: /usr/bin/java -version 2> /tmp/j.log – Rhand May 15 '12 at 13:29
  • 1
    bugs.sun.com/bugdatabase/view_bug.do?bug_id=4380614 We should think very, very carefully before ever fixing this bug. It's obviously the wrong thing to print version information to stderr, but since we've been doing that since the beginning of time it seems likely that we'll break existing systems built on top of Java if we change it now. If we do decide to change this then it must wait until the Tiger release so that adequate testing can be done. – Rhand May 15 '12 at 13:37
  • 1
    From bash manual: There are two formats for redirecting standard output and standard error: &>word and >&word Of the two forms, the first is preferred. This is semantically equivalent to >word 2>&1 – us3r May 15 '12 at 13:52

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.