jar服务配置文件

  1. JAVA_HOME = /usr/local/jdk1.8.0_91/
  2. work_dir=/root
  3. jar_file = rest-demo.jar
  4. log = app.log
  5. args = "--server.port=9090 --spring.profiles.active=dev"
  6. #options = "-Xms128m -Xmx256m"

启动脚本

jar.server rest-demo.conf start

  1. #!/bin/bash
  2. config_file=$1
  3. cmd=$2
  4. # 全局变量,用来接收配置文件的值
  5. JAVA_HOME=
  6. work_dir=
  7. jar_file=
  8. log=
  9. args=
  10. options=
  11. # 使用说明
  12. function usage(){
  13. echo "input error !"
  14. echo "Usage: `basename $0` (config_file) (start|stop|restart|status|log)"
  15. exit -1
  16. }
  17. # 读取配置文件
  18. function read_conf(){
  19. tmp=`grep '^JAVA_HOME' $config_file`
  20. JAVA_HOME=`echo ${tmp#*=} |tr -d '\n\r' | sed 's/\"//g'`
  21. tmp=`grep '^work_dir' $config_file`
  22. work_dir=`echo ${tmp#*=} |tr -d '\n\r' | sed 's/\"//g'`
  23. tmp=`grep '^jar_file' $config_file`
  24. jar_file=`echo ${tmp#*=} |tr -d '\n\r' | sed 's/\"//g'`
  25. tmp=`grep '^log' $config_file`
  26. log=`echo ${tmp#*=} |tr -d '\n\r' | sed 's/\"//g'`
  27. tmp=`grep '^args' $config_file`
  28. args=`echo ${tmp#*=} |tr -d '\n\r' | sed 's/\"//g'`
  29. tmp=`grep '^options' $config_file`
  30. options=`echo ${tmp#*=} |tr -d '\n\r' | sed 's/\"//g'`
  31. # 判断目录是否存在
  32. if [[ -d $work_dir ]]; then
  33. cd $work_dir
  34. fi
  35. # 判断文件是否存在
  36. if [[ ! -f $jar_file ]]; then
  37. echo "${jar_file} is not exist !"
  38. exit -1
  39. fi
  40. }
  41. # 获取pid
  42. function get_pid(){
  43. pid=`ps -ef|grep -v grep |grep -v $0 |grep $jar_file |awk '{print $2}'`
  44. }
  45. # 启动
  46. function start(){
  47. get_pid
  48. if [[ $pid ]]; then
  49. echo "service already exists,do not repeat startup !"
  50. exit -1
  51. fi
  52. if [[ ${JAVA_HOME} ]];then
  53. command=${JAVA_HOME}/bin/java
  54. else
  55. command=java
  56. fi
  57. ${command} -version &> /dev/null
  58. if [[ $? != 0 ]];then
  59. echo "please configure JAVA_HOME !"
  60. exit -1
  61. fi
  62. nohup ${command} $options -jar ${jar_file} ${args} &> ${log} &
  63. }
  64. # 停止
  65. function stop(){
  66. get_pid
  67. if [[ $pid ]]; then
  68. /bin/kill -9 $pid
  69. fi
  70. sleep 1
  71. status
  72. }
  73. # 重启
  74. function restart(){
  75. get_pid
  76. if [[ $pid ]]; then
  77. /bin/kill -9 $pid
  78. fi
  79. start
  80. }
  81. # 查看状态
  82. function status(){
  83. get_pid
  84. [[ $pid ]] && echo "service is started,pid is $pid" || echo "service is stopped"
  85. }
  86. # 查看日志
  87. function log(){
  88. tailf -100 $log
  89. }
  90. # 读取配置文件
  91. read_conf
  92. case $cmd in
  93. "start")
  94. start
  95. ;;
  96. "stop")
  97. stop
  98. ;;
  99. "status")
  100. status
  101. ;;
  102. "restart")
  103. restart
  104. ;;
  105. "log")
  106. log
  107. ;;
  108. *)
  109. usage
  110. ;;
  111. esac

使用

jar.server rest-demo.conf start

配置文件:可以是绝对路径或相对路径

配置计划任务,实现失败启动

crontab -e

  1. jar.server