Why CentOS does not stop your init script

Introduction

If you created a simple init script below /etc/init.d, whichgets started, but not stopped on reboot or system halt, thisarticle is for you.

Background

I assume you ensured that the chkconfig information in thescript is correct and that you ran chkconfig $name on. The outputof chkconfig should look like this:

[root@kvm-hw-snr01 ~]# chkconfig --list  | grep kvm-vms
kvm-vms         0:off   1:off   2:on    3:on    4:on    5:on    6:off
[root@kvm-hw-snr01 ~]# 

Although this looks correct, there is a small block in /etc/rc.d/rcthat prevents your init script from being called on stop:

# First, run the KILL scripts.
for i in /etc/rc$runlevel.d/K* ; do

        # Check if the subsystem is already up.
        subsys=${i#/etc/rc$runlevel.d/K??}
        [ -f /var/lock/subsys/$subsys -o -f /var/lock/subsys/$subsys.init ] || continue
        check_runlevel "$i" || continue

        # Bring the subsystem down.
        [ -n "$UPSTART" ] && initctl emit --quiet stopping JOB=$subsys
        $i stop
        [ -n "$UPSTART" ] && initctl emit --quiet stopped JOB=$subsys
done

So only if your script creates/var/lock/subsys/yourscriptname or /var/lock/subsys/yourscriptname.init,it will be called on stop.

Solution

You can include the following three lines into your script to getyour script stopped:

broken_lock_file_for_centos=/var/lock/subsys/kvm-vms

# In the start block
touch "$broken_lock_file_for_centos"

# In the stop block
rm -f "$broken_lock_file_for_centos"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值