2018-02-07 17:14:35 +08:00
|
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
|
|
#################################################################################
|
|
|
|
|
# JAVA 应用通用启动脚本
|
2019-05-07 14:29:16 +08:00
|
|
|
|
# @author: Zhang Peng
|
2018-02-07 17:14:35 +08:00
|
|
|
|
#################################################################################
|
|
|
|
|
|
|
|
|
|
# 检查脚本参数,如必要参数未传入,退出脚本。
|
|
|
|
|
function checkInput() {
|
2019-10-29 18:22:19 +08:00
|
|
|
|
if [ "${app}" == "" ] || [ "${oper}" == "" ] || [ "${javaArgs}" == "" ] || [ "${classpathArgs}" == "" ] || [ "${bootstrapClass}" == "" ]; then
|
|
|
|
|
echo "请输入脚本参数:app oper javaArgs classpathArgs bootstrapClass"
|
|
|
|
|
echo " app: 应用名。"
|
|
|
|
|
echo " oper: 运行环境(必填)。可选值:start|stop|restart"
|
|
|
|
|
echo " javaArgs: JVM 参数(必填)。"
|
|
|
|
|
echo " classpathArgs: classpath参数(必填)。"
|
|
|
|
|
echo " bootstrapClass: 启动类(必填)。"
|
|
|
|
|
exit 0
|
|
|
|
|
fi
|
2018-02-07 17:14:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# 检查文件夹是否存在,不存在则创建
|
|
|
|
|
function createFolderIfNotExist() {
|
2019-10-29 18:22:19 +08:00
|
|
|
|
if [ ! -d "$1" ]; then
|
|
|
|
|
mkdir -p "$1"
|
|
|
|
|
fi
|
2018-02-07 17:14:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# 检查服务是否已经启动
|
|
|
|
|
pids=""
|
|
|
|
|
function checkStarted() {
|
2019-10-29 18:22:19 +08:00
|
|
|
|
pids=`ps -ef | grep java | grep ${app} | awk '{print $2}'`
|
|
|
|
|
if [ -n "${pids}" ]; then
|
|
|
|
|
return 0
|
|
|
|
|
else
|
|
|
|
|
return 1
|
|
|
|
|
fi
|
2018-02-07 17:14:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function main() {
|
2019-10-29 18:22:19 +08:00
|
|
|
|
case "${oper}" in
|
|
|
|
|
start)
|
|
|
|
|
echo -n "starting server: "
|
|
|
|
|
# 检查服务是否已经启动
|
|
|
|
|
if checkStarted; then
|
|
|
|
|
echo "ERROR: server already started!"
|
|
|
|
|
echo "PID: ${pids}"
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
2018-02-07 17:14:35 +08:00
|
|
|
|
|
2019-10-29 18:22:19 +08:00
|
|
|
|
args="${javaArgs} -classpath ${classpathArgs} ${bootstrapClass}"
|
|
|
|
|
echo -e "statup params:\n ${args}"
|
2018-02-07 17:14:35 +08:00
|
|
|
|
|
2019-10-29 18:22:19 +08:00
|
|
|
|
#启动服务
|
|
|
|
|
touch ${LOG_DIR}/${app}-startup.log
|
|
|
|
|
nohup java ${args} > ${LOG_DIR}/${app}-startup.log 2>&1 &
|
|
|
|
|
# echo -e "执行参数:\n${args}"
|
|
|
|
|
echo -e "\nthe server is started..."
|
|
|
|
|
;;
|
|
|
|
|
stop)
|
|
|
|
|
echo -n "stopping server: "
|
|
|
|
|
#dubbo提供优雅停机, 不能使用kill -9
|
|
|
|
|
if checkStarted; then
|
|
|
|
|
kill ${pids}
|
|
|
|
|
echo -e "\nthe server is stopped..."
|
|
|
|
|
else
|
|
|
|
|
echo -e "\nno server to be stopped..."
|
|
|
|
|
fi
|
|
|
|
|
;;
|
|
|
|
|
restart)
|
|
|
|
|
$0 ${app} stop "${javaArgs}" "${classpathArgs}" "${bootstrapClass}"
|
|
|
|
|
sleep 5
|
|
|
|
|
$0 ${app} start "${javaArgs}" "${classpathArgs}" "${bootstrapClass}"
|
|
|
|
|
;;
|
|
|
|
|
*)
|
|
|
|
|
echo "Invalid oper: ${oper}."
|
|
|
|
|
exit 1
|
|
|
|
|
esac
|
2018-02-07 17:14:35 +08:00
|
|
|
|
|
2019-10-29 18:22:19 +08:00
|
|
|
|
exit 0
|
2018-02-07 17:14:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
######################################## MAIN ########################################
|
|
|
|
|
# 设置环境变量
|
|
|
|
|
export LANG="zh_CN.UTF-8"
|
|
|
|
|
|
|
|
|
|
# 获取输入参数
|
|
|
|
|
app=`echo $1`
|
|
|
|
|
oper=`echo $2`
|
|
|
|
|
javaArgs=`echo $3`
|
|
|
|
|
classpathArgs=`echo $4`
|
|
|
|
|
bootstrapClass=`echo $5`
|
|
|
|
|
vars=$*
|
|
|
|
|
checkInput
|
|
|
|
|
|
|
|
|
|
# 设置全局常量
|
|
|
|
|
LOG_DIR=/home/zp/log
|
|
|
|
|
createFolderIfNotExist ${LOG_DIR}
|
|
|
|
|
|
|
|
|
|
# 执行 main 方法
|
|
|
|
|
main
|