Ruchi
Ruchi

Reputation: 5192

crontab doesnt work for run java class

testjob.sh

#!/bin/bash
export JAVA_HOME=/usr/java/jdk1.6.0_07
echo "Java Home is $JAVA_HOME"
export CLASSPATH=.:..:$CLASSPATH:
echo "Path is is $PATH"
echo "CLASSPATH is is $CLASSPATH"
$JAVA_HOME/bin/java  TestJob
echo "$JAVA_HOME/bin/java  TestJob"

crontab -e

* * * * * /usr/testjob.sh  >> /usr/result.txt 2>&1

if i run shell script manually it runs fine but when it will run through crontab job, error will occur as class not found..

please suggest..

Upvotes: 0

Views: 10985

Answers (2)

beny23
beny23

Reputation: 35038

Your classpath is set as "." and "..", which means the current directory and it's parent directory. So when you run it locally, you'll have to be in a particular directory for it to work.

Try setting the classpath to an absolute directory in your script.

To check which directory is the current directory you may also want to put

echo "Current directory: `pwd`"

into your testjob.sh script to illustrate the differences when invoking "manually" and through crontab.

Upvotes: 1

Ravi Bhatt
Ravi Bhatt

Reputation: 3163

Have a look at this. Should answer your question

Where can I set environment variables that crontab will use?

Again read this http://linuxshellaccount.blogspot.com/2007/10/crontab-and-your-environment.html\

The easiest way you can make sure that you have same environment in cron as you have when running any script as the regular user is to "source" the environment into the script by adding a line like:

. /etc/profile . /home/user/.profile

to the top of your script (below the #! line). The literal dot, space, filename patterns tells your shell to read in all variables in that named file, so you could run your cron job with the same environment as when you test it manually, which might avoid issues caused by points 1 and 2 above.

Upvotes: 3

Related Questions