crontab doesnt work for run java class

廉价感情. 提交于 2019-12-25 05:15:10

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/7979661/crontab-doesnt-work-for-run-java-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!