zoukankan      html  css  js  c++  java
  • CentOS配置jar应用程序开机启动的方法

    1.背景

      某些java程序,我们需要开机,自动启动....

    2.实现步骤

    2.1.编写程序启动脚本boot.sh

    #!/bin/bash
    #这里可替换为你自己的执行程序,其他代码无需更改
    APP_NAME=DNSProxy.jar
    
    #使用说明,用来提示输入参数
    usage() {
     echo "Usage: sh 脚本名.sh [start|stop|restart|status]"
     exit 1
    }
      
    #检查程序是否在运行
    is_exist(){
    pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}' `
    #如果不存在返回1,存在返回0     
    if [ -z "${pid}" ]; then
    return 1
    else
     return 0
    fi
    }
    
    #启动方法
    start(){
    is_exist
    if [ $? -eq "0" ]; then
     echo "${APP_NAME} is already running. pid=${pid} ."
    else
     nohup java -jar $APP_NAME > /dev/null 2>&1 &
     echo "${APP_NAME} start success"
    fi
    }
    
    #停止方法
    stop(){
    is_exist
    if [ $? -eq "0" ]; then
     kill -9 $pid
    else
     echo "${APP_NAME} is not running"
    fi  
    }
    
    #输出运行状态
    status(){
    is_exist
    if [ $? -eq "0" ]; then
     echo "${APP_NAME} is running. Pid is ${pid}"
    else
     echo "${APP_NAME} is NOT running."
    fi
    }
    
    #重启
    restart(){
    stop
    start
    }
    
    #根据输入参数,选择执行对应方法,不输入则执行使用说明
    case "$1" in
    "start")
     start
     ;;
    "stop")
     stop
     ;;
    "status")
     status
     ;;
    "restart")
     restart
     ;;
    *)
     usage
     ;;
    esac
    View Code

      写完后必须设置权限为可执行

      chmod +x boot.sh

    2.2.修改文件rc.local

      文件位置/etc/rc.d/rc.local

      在文件末尾加上启动脚本执行命令,这里写自己的启动脚本

      su - root -c '/usr/app/boot.sh start'

    2.3.修改rc.local为可以执行的文件

      文件位置/etc/rc.d/rc.local

      chmod +x /etc/rc.d/rc.local

    3.测试

      执行命令:reboot

      重启Linux系统,等系统重启后,检查程序是否启动

      完美!

  • 相关阅读:
    Access restriction: The type BASE64Encoder is not accessible due to restrict(转载)
    ECLIPSE控制台信息导出
    ZUI分页器的使用案例(ECLIPSE SMS项目)
    关于PLSQL启动用时较长的问题解决
    javax.naming.NamingException: Cannot create resource instance报错修改
    百度AI人脸识别的学习总结
    ECharts3.0饼状图使用问题总结
    jni开发
    AndroidStudio封装jar并混淆
    Androidstudio代码混淆
  • 原文地址:https://www.cnblogs.com/newAndHui/p/11899900.html
Copyright © 2011-2022 走看看