templates.jar.sh.ftl Maven / Gradle / Ivy
#!/bin/bash
Active=${project.active}
jar_name=${project.artifactId}-${project.version}.jar
jar_path=/opt/${project.artifactId}
log_path=/opt/${project.artifactId}/`date +%y-%m-%d`_service_out.log
#指向自定义jdk
export JAVA_HOME=/opt/jdk-17
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$JAVA_HOME/bin:$PATH
stop() {
echo ""
docker stop ${project.artifactId}-${project.version}
docker rm ${project.artifactId}-${project.version}
docker rmi ${cloudImage.registry}/${cloudImage.namespace}/${project.artifactId}:${project.version}
pid=$(ps -ef | grep $jar_name | grep -v grep | awk '{print $2}')
if [ -z $pid ]; then # -z 是判断是否为空
echo "${r'${jar_name}'}未运行!"
else
echo "准备关闭进程$pid"
kill -9 $pid
sleep 0.5
echo "${r'${jar_name}'}已关闭!"
fi
echo ""
}
start() {
echo ""
pid=$(ps -ef | grep $jar_name | grep -v grep | awk '{print $2}')
if [ -z $pid ]; then
cd $jar_path # 这里转到jar包目录执行命令,是为了使用jar_path下的config文件,貌似java程序只能识别当前执行命令目录下的配置,否则就是使用已打入Jar包的配置文件。
nohup java -jar ${project.jvmParameter} -DNACOS_ACTIVE=$Active -DACTIVE=$Active ${project.jarParameter} $jar_name >$log_path 2>&1 &
pid=$(ps -ef | grep $jar_name | grep -v grep | awk '{print $2}')
echo "应用 ${r'${jar_name}'}启动成功,pid=${r'${pid}'}"
else
echo "应用 ${r'${jar_name}'} 正在运行,pid = ${r'${pid}'}"
fi
echo ""
}
status() {
echo ""
pid=$(ps -ef | grep $jar_name | grep -v grep | awk '{print $2}')
if [ -z $pid ]; then
echo "应用 ${r'${jar_name}'} 未运行"
else
echo "应用 ${r'${jar_name}'} 正在运行,pid = ${r'${pid}'}"
fi
echo ""
}
restart() {
stop
sleep 2
echo "停止完成,准备启动jar包"
start
}
#使用说明,用来提示输入参数
usage() {
echo "Usage: sh 执行脚本.sh [start|stop|restart|status]"
exit 1
}
#根据输入参数,选择执行对应方法,不输入则执行使用说明
case "$1" in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
*)
usage
;;
esac