linux定时任务生产java服务无法执行问题案例

我写了一个重启resin的脚本,由于业务原因,需要定时在某一个时间重启下resin服务器

于是就在crontab里配置了如下内容:

50 17 * * 1-5 root /usr/local/bin/resin_restart.sh

其中,resin_restart.sh内容如下:

1 #!/bin/sh
2 /usr/local/bin/xxresin_stop.sh
3 /usr/local/bin/xxresin_start.sh

有问题的时刻到来了,服务器虽然定时起来了,但是却报了如下错误:

Resin can't load com.sun.tools.javac.Main.  Usually this means that the JDK tools.jar is missing from the classpath, 
possibly because of using a JRE instead of the JDK.  
You can either add tools.jar to the classpath or change the compiler to an external one with <java compiler='javac'/> or jikes.

但是,明明已经在profile里配置了环境变量,为啥还找不到呢。折腾了需求没有搞定。

分析如下:

1 由于export变量问题导致:具体为,crontab执行shell时只能识别为数不多的系统环境变量,
2 普通环境变量一般是无法识别的,如果在编写的脚本中需要使用变量,最好使用export重新声明下该变量,
3 以确保脚本正确执行。以后作为一个开发基本规范写上。

然后我在resin重启脚本里重新定义了下环境变量,脚本如下:

#!/bin/sh
#下面就是环境变量定义
JAVA_HOME="/opt/jdk1.6.0_18"
CLASSPATH=$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
PATH=$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:/opt/nginx-0.7.61/sbin:/opt/jdk1.6.0_18/bin:/opt/resin-3.0.25/bin:$PATH
export JAVA_HOME PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC CLASSPATH
/usr/local/bin/xxresin_stop.sh
/usr/local/bin/xxresin_start.sh

经过测试,定时任务此时顺利重启

原文地址:https://www.cnblogs.com/luoahong/p/6197564.html