美文网首页
Centos7 开机自启动脚本

Centos7 开机自启动脚本

作者: 轻轻敲醒沉睡的心灵 | 来源:发表于2023-11-28 17:43 被阅读0次

    做了java项目,jar包启动脚本已经写好了。想让他开机自启,做下记录:

    1. 要自启的脚本

    脚本位置任意,就叫 app_all_start.sh吧。暂放root目录下吧。脚本要有权限哦。

    #!/bin/bash
    #chkconfig: 35 20 80
    #description: jar start
    
    export JAVA_HOME=/usr/local/jdk1.8.0_381
    export JRE_HOME=${JAVA_HOME}/jre
    export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
    export PATH=${JAVA_HOME}/bin:$PATH
    
    echo "--------wait for mysql、redis、nacos--------------"
    sleep 180s
    
    ## 多个jar包都要启动
    /opt/workspace/test1/start.sh
    sleep 20s
    /opt/workspace/test2/start.sh
    sleep 20s
    /opt/workspace/test3/start.sh
    sleep 20s
    /opt/workspace/test4/start.sh
    
    

    2. 单个jar 包 启动文件

    就是上面那个start.sh文件

    #!/bin/bash
    
    export JAVA_HOME=/usr/local/jdk1.8.0_381
    export JRE_HOME=${JAVA_HOME}/jre
    export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
    export PATH=${JAVA_HOME}/bin:$PATH
    
    nohup java -jar -Xmx2g -Xms2g  -Dfile.encoding=utf-8 -Dspring.config.location=classpath:/application-test.yml /opt/workspace/test1/test1.jar >/dev/null 2>&1 &
    

    3. app_all_start.sh自启

    • 修改rc.local文件
    vim  /etc/rc.d/rc.local
    

    在最后添加自启的脚本

    #!/bin/bash
    # THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
    #
    # It is highly advisable to create own systemd services or udev rules
    # to run scripts during boot instead of using this file.
    #
    # In contrast to previous versions due to parallel execution during boot
    # this script will NOT be run after all other services.
    #
    # Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
    # that this script will be executed during boot.
     
    touch /var/lock/subsys/local
     
    /root/app_all_start.sh
    
    • rc.local添加执行权限
    chmod +x /etc/rc.d/rc.local
    
    • 重启服务器测试

    相关文章

      网友评论

          本文标题:Centos7 开机自启动脚本

          本文链接:https://www.haomeiwen.com/subject/ukkxgdtx.html