1 .run.sh 文件内容,如下,加粗地方需要替换成自己的项目路径
#!/bin/sh
#Java的安装目录
JAVA_HOME=/usr/java/jdk1.8.0_211-amd64
#运行程序所使用的用户
OWNER=root
#Java程序的目录
APP_HOME=/home/lfserver
#Main方法的类
APP_JAR=app/app-opensite-provider.jar
#日志文件
LOG_FILE=/home/lfserver/app/app.log
#设置CLASSPATH
CLASSPATH=
JAVA_CMD=
#循环将lib文件夹下所有的jar添加到CLASSPATH
for i in "$APP_HOME"/lib/*.jar; do
CLASSPATH="$CLASSPATH":"$i"
done
#设置运行参数
JAVA_OPTS="-Xms512m -Xmx512m -Xmn256m"
#echo $CLASSPATH;
psid=0
#检查Java程序是否运行
checkpid(){
javaps=`$JAVA_HOME/bin/jps -l | grep $APP_HOME/$APP_JAR`
if [ -n "$javaps" ];then
psid=`echo $javaps | awk '{print $1}'`
else
psid=0
fi
}
#运行程序
start(){
checkpid
if [ $psid -ne 0 ];then
echo "WARN:$APP_JAR already started!(pid=$psid)"
else
echo "Starting $APP_JAR..."
JAVA_CMD=nohup java $JAVA_OPTS -jar $APP_HOME/$APP_JAR 1 > $LOG_FILE &
echo "JAVA_CMD"
$JAVA_CMD
checkpid
if [ $psid -ne 0 ];then
echo "Started $APP_JAR (pid=$psid)[OK]"
else
echo "Started $APP_JAR [FAILED]"
fi
fi
}
#停止程序
stop(){
checkpid
if [ $psid -ne 0 ];then
echo "Stoping $APP_JAR...(pid=$psid)"
su - $OWNER -c "kill $psid"
checkpid
if [ $psid -ne 0 ];then
echo "Stoping use kill -9"
su - $OWNER -c "kill -9 $psid"
fi
checkpid
if [ $psid -eq 0 ];then
echo "Stoped $APP_JAR [OK]"
else
echo "Stoped $APP_JAR [Failed]"
stop
fi
else
echo "WARN:$APP_JAR is not runing"
fi
}
#查看状态
status(){
checkpid
if [ $psid -ne 0 ];then
echo "$APP_JAR is runing (pid=$psid)"
else
echo "$APP_JAR is not runing"
fi
}
#帮助信息
info() {
echo "System Information:"
echo "****************************"
echo `head -n 1 /etc/issue`
echo `uname -a`
echo
echo "JAVA_HOME=$JAVA_HOME"
echo `$JAVA_HOME/bin/java -version`
echo
echo "APP_HOME=$APP_HOME"
echo "APP_MAINCLASS=$APP_JAR"
echo "****************************"
}
#$1表示接收第一个参数,如 ./run.sh start 。则$1就是start
case "$1" in
'app')
APP_JAR=$APP_JAR
LOG_FILE=$LOG_FILE
;;
*)
echo "Usage: $0 {app}"
exit 1
esac
#$1表示接收第一个参数,如 ./run.sh start second 。则$2就是second
case "$2" in
'start')
start
;;
'stop')
stop
;;
'restart')
stop
start
;;
'info')
info
;;
'status')
status
;;
*)
echo "Usage: $0 {start|stop|restart|status|info}"
exit 1
esac
exit 0;