本來想將寫的代碼掛在crontab下運行,誰知道無法運行,沒有任何輸出,試着用ls -al >> 1.log試了一下,確定crontab是正常運行的。
從網站上找了下問題,原因出在crontab啟動的程序並不會加載環境變量,因此像“java”這種命令是無法啟動的。
解決方案就是講java換做具體的java路徑。
步驟如下:
1. 運行命令crontab -e加入要運行的命令,並配置其運行頻次
30 01 * * * /opt/changedig/app/module/rcproject/rcproject.sh
2. 修改啟動腳本,將java路徑換做絕對路徑
#!/bin/sh WORK_DIR="/opt/changedig/app/module/rcproject" MYNAME="rcproject" pidfile=.$MYNAME.pid if test -f $WORK_DIR/$pidfile then # See if a process is running with that process id pid=`cat $WORK_DIR/$pidfile` if test -n "$pid" then ps -ef|grep $MYNAME|grep -v grep|awk '{print $2}'|grep $pid > /dev/null if test $? -eq 0 then # The process is running ! printf "The process is running !\n" exit 3 else # Try the ps listing again as it is not always reliable ps -ef|grep $MYNAME|grep -v grep|awk '{print $2}'|grep $pid > /dev/null if test $? -eq 0 then # The process is running ! printf "The process is running !\n" exit 3 fi fi fi fi echo $$>$WORK_DIR/$pidfile # Check process is existence if [ ! -f $WORK_DIR/rcproject-0.0.1.jar ] ; then printf "Error: Can not find the file $WORK_DIR/rcproject-*.jar\n" return 2 fi /usr/local/java/jdk1.7.0_45/bin/java -jar $WORK_DIR/rcproject-0.0.1.jar
如此即可