CentOS7.4系统重启时执行自定义脚本

  1. demo脚本文件内容
#!/usr/bin/env bash
# chkconfig: 123456 10 90 
# description: myservice ....
filename="/usr/local/jar/demo.jar"
logpath="/usr/local/jar/temp.log"
pid=`ps -ef | grep ${filename} | grep -v grep | awk '{print $2}'`
if [ -z "${pid}" ]; then
    echo "begin start"
    # 这里一定要用全路径,直接写java系统重启后找不到
    nohup /usr/local/java/bin/java -jar ${filename} >> ${logpath} 2>&1 &
else
    echo ${pid}
    echo "starting"
fi

chkconfig 的123456说明:
缺省的运行级,RHS用到的级别如下:
0:关机
1:单用户模式
2:无网络支持的多用户模式
3:有网络支持的多用户模式
4:保留,未使用
5:有网络支持有X-Window支持的多用户模式
6:重新引导系统,即重启
123456后面的10 90说明:
对各个运行级的详细解释:
0 为停机,机器关闭。
1 为单用户模式,就像Win9x下的安全模式类似。
2 为多用户模式,但是没有NFS支持。
3 为完整的多用户模式,是标准的运行级。
4 一般不用,在一些特殊情况下可以用它来做一些事情。例如在笔记本 电脑的电池用尽时,可以切换到这个模式来做一些设置。
5 就是X11,进到X Window系统了。
6 为重启,运行init 6机器就会重启。

  1. 将文件放在 /etc/rc.d/init.d/文件夹下面
  2. 赋予脚本执行权限,chmod +x demo.sh
  3. 增加开机启动项
chkconfig --add demo.sh
chkconfig demo.sh on
  1. 修改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

# 重启jar包,千万注意别写错路径
/etc/rc.d/init.d/demo.sh

  1. 赋予rc.local文件执行权限,chmod +x rc.local
  2. 验证demo.sh文件是否开机自动启动

  1. 重启验证就可以了,最后嘱咐大家一句,一定要每次去查看重启后的运行脚本中自己输出的日志,因为有可能我们的情况不一样。欢迎各位及时留下宝贵的意见,我们共同讨论、进步。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菜鸟特工007

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值