jar服务配置文件
JAVA_HOME = /usr/local/jdk1.8.0_91/work_dir=/rootjar_file = rest-demo.jarlog = app.logargs = "--server.port=9090 --spring.profiles.active=dev"#options = "-Xms128m -Xmx256m"
启动脚本
jar.server rest-demo.conf start
#!/bin/bashconfig_file=$1cmd=$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 ]]; thencd $work_dirfi# 判断文件是否存在if [[ ! -f $jar_file ]]; thenecho "${jar_file} is not exist !"exit -1fi}# 获取pidfunction get_pid(){pid=`ps -ef|grep -v grep |grep -v $0 |grep $jar_file |awk '{print $2}'`}# 启动function start(){get_pidif [[ $pid ]]; thenecho "service already exists,do not repeat startup !"exit -1fiif [[ ${JAVA_HOME} ]];thencommand=${JAVA_HOME}/bin/javaelsecommand=javafi${command} -version &> /dev/nullif [[ $? != 0 ]];thenecho "please configure JAVA_HOME !"exit -1finohup ${command} $options -jar ${jar_file} ${args} &> ${log} &}# 停止function stop(){get_pidif [[ $pid ]]; then/bin/kill -9 $pidfisleep 1status}# 重启function restart(){get_pidif [[ $pid ]]; then/bin/kill -9 $pidfistart}# 查看状态function status(){get_pid[[ $pid ]] && echo "service is started,pid is $pid" || echo "service is stopped"}# 查看日志function log(){tailf -100 $log}# 读取配置文件read_confcase $cmd in"start")start;;"stop")stop;;"status")status;;"restart")restart;;"log")log;;*)usage;;esac
使用
jar.server rest-demo.conf start
配置计划任务,实现失败启动
crontab -e
jar.server
