Linux crontab doesnt work for run java class

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7979661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 06:58:16  来源:igfitidea点击:

crontab doesnt work for run java class

javalinuxcrontab

提问by Sweety

testjob.sh

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

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..

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..

please suggest..

回答by Ravi Bhatt

Have a look at this. Should answer your question

Have a look at this. Should answer your question

Where can I set environment variables that crontab will use?

Where can I set environment variables that crontab will use?

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

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:

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

. /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.

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.

回答by beny23

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.

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.

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

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

echo "Current directory: `pwd`"

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

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