jar服务配置文件
JAVA_HOME = /usr/local/jdk1.8.0_91/
work_dir=/root
jar_file = rest-demo.jar
log = app.log
args = "--server.port=9090 --spring.profiles.active=dev"
#options = "-Xms128m -Xmx256m"
启动脚本
jar.server rest-demo.conf start
#!/bin/bash
config_file=$1
cmd=$2
# 全局变量,用来接收配置文件的值
JAVA_HOME=
work_dir=
jar_file=
log=
args=
options=
# 使用说明
function usage(){
echo "input error !"
echo "Usage: `basename $0` (config_file) (start|stop|restart|status|log)"
exit -1
}
# 读取配置文件
function read_conf(){
tmp=`grep '^JAVA_HOME' $config_file`
JAVA_HOME=`echo ${tmp#*=} |tr -d '\n\r' | sed 's/\"//g'`
tmp=`grep '^work_dir' $config_file`
work_dir=`echo ${tmp#*=} |tr -d '\n\r' | sed 's/\"//g'`
tmp=`grep '^jar_file' $config_file`
jar_file=`echo ${tmp#*=} |tr -d '\n\r' | sed 's/\"//g'`
tmp=`grep '^log' $config_file`
log=`echo ${tmp#*=} |tr -d '\n\r' | sed 's/\"//g'`
tmp=`grep '^args' $config_file`
args=`echo ${tmp#*=} |tr -d '\n\r' | sed 's/\"//g'`
tmp=`grep '^options' $config_file`
options=`echo ${tmp#*=} |tr -d '\n\r' | sed 's/\"//g'`
# 判断目录是否存在
if [[ -d $work_dir ]]; then
cd $work_dir
fi
# 判断文件是否存在
if [[ ! -f $jar_file ]]; then
echo "${jar_file} is not exist !"
exit -1
fi
}
# 获取pid
function get_pid(){
pid=`ps -ef|grep -v grep |grep -v $0 |grep $jar_file |awk '{print $2}'`
}
# 启动
function start(){
get_pid
if [[ $pid ]]; then
echo "service already exists,do not repeat startup !"
exit -1
fi
if [[ ${JAVA_HOME} ]];then
command=${JAVA_HOME}/bin/java
else
command=java
fi
${command} -version &> /dev/null
if [[ $? != 0 ]];then
echo "please configure JAVA_HOME !"
exit -1
fi
nohup ${command} $options -jar ${jar_file} ${args} &> ${log} &
}
# 停止
function stop(){
get_pid
if [[ $pid ]]; then
/bin/kill -9 $pid
fi
sleep 1
status
}
# 重启
function restart(){
get_pid
if [[ $pid ]]; then
/bin/kill -9 $pid
fi
start
}
# 查看状态
function status(){
get_pid
[[ $pid ]] && echo "service is started,pid is $pid" || echo "service is stopped"
}
# 查看日志
function log(){
tailf -100 $log
}
# 读取配置文件
read_conf
case $cmd in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
"log")
log
;;
*)
usage
;;
esac
使用
jar.server rest-demo.conf start
配置计划任务,实现失败启动
crontab -e
jar.server