Centos7 jar服务开机自动启动

在linux服务器下,出现服务器断电、服务器重启等情况,这时候就需要一些服务开机自启动。
环境:centos7虚拟机、jar包、jdk8
前提:
1、centos7已经配置jdk8环境

2、上传jar包、新建脚本文件、日志文件等

2.1、新建启动脚本 jar-cachfile-service-start.sh

#!/bin/sh

rt JAVA_HOME=/etc/alternatives/jre_1.8.0_openjdk
export PATH=$JAVA_HOME/bin:$PATH

nohup java -jar /LazmProject/cachfile_jar/cachfile.jar  --spring.config.location=/LazmProject/conf/cachfile.properties   >/LazmProject/logs/cachfile.log &

echo $! > /data/jarservice/db-cachfile-service.pid

2.2、新建停止脚本jar-cachfile-service-stop.sh

#!/bin/sh

PID=$(cat /data/jarservice/db-cachfile-service.pid)
kill -9 $PID

3、增加脚本执行权限

chmod +x  jar-cachfile-service-start.sh
chmod +x  jar-cachfile-service-stop.sh

4、启动脚本

#启动服务

./data/jarservice/jar-cachfile-service-start.sh

#停止服务

./data/jarservice/jar-cachfile-service-stop.sh

5、添加开机自动启动

vi /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
/data/jarservice/jar-cachfile-service-start.sh

6、设置rc.local 可执行权限

centos7下rc.local的权限被降低,需要手动为其赋予可执行权限

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

7、reboot重启下服务器看下结果,是否自动启动了jar包

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值