/etc/rc.d/init.d目录下启动自定义脚本进程名的区别与添加守护进程

目录

/etc/rc.d/init.d目录下启动自定义脚本进程名的区别

/etc/rc.d/init.d目录下启用守护进程脚本


/etc/rc.d/init.d目录下启动自定义脚本进程名的区别

/etc/rc.d/init.d目录下的script1脚本内不运行别的脚本,那么service script1 start执行后,生成进程名/etc/rc.d/init.d/script1 start
/etc/rc.d/init.d目录下的script2脚本内运行别的脚本/root/script3.sh(必需是sh /root/script3.sh),那么service script2 start执行后,生成进程名sh /root/script3.sh
生成进程名/etc/rc.d/init.d/script1 start实例
[root@centos7 ~]# cat /etc/rc.d/init.d/ceshiqi 
#! /bin/bash
#
#
# chkconfig: 2345 40 30
# description:this is a test
#
# Source function library.
. /etc/init.d/functions
function start() {
exec >/root/1.txt
for value in `seq 100`
do
echo $value 
sleep 5
done
}
case "$1" in 
start)
start &
;;
stop)
ps aux|grep ceshiqi|grep -v grep|awk '{print $2}'|xargs kill -9  &>/dev/null          #进程名ceshiqi
;;
reload|restart)
ps aux|grep ceshiqi|grep -v grep|awk '{print $2}'|xargs kill -9  &>/dev/null
start &
;;
*) 
echo "usage:ceshiqi restart|start|stop"
;;
esac
exit 0
[root@centos7 ~]# chmod +x /etc/rc.d/init.d/ceshiqi
[root@centos7 ~]# ps aux|grep ceshiqi
root       1956  0.0  0.0 112808   968 pts/0    S+   16:16   0:00 grep --color=auto ceshiqi
[root@centos7 ~]# service ceshiqi start
Starting ceshiqi (via systemctl):                          [  OK  ]
[root@centos7 ~]# ps aux|grep ceshiqi
root       1981  0.0  0.0 115540   980 ?        S    16:16   0:00 /bin/bash /etc/rc.d/init.d/ceshiqi start
root       1985  0.0  0.0 112808   968 pts/0    S+   16:16   0:00 grep --color=auto ceshiqi
[root@centos7 ~]# tail -f /root/1.txt
1
2
3
^C
[root@centos7 ~]# service ceshiqi stop
Stopping ceshiqi (via systemctl):  Killed
[root@centos7 ~]# ps aux|grep ceshi
root       2028  0.0  0.0 112808   964 pts/0    S+   16:17   0:00 grep --color=auto ceshi
[root@centos7 ~]# service ceshiqi restart
Restarting ceshiqi (via systemctl):                        [  OK  ]
[root@centos7 ~]# ps aux|grep ceshi
root       2052  0.0  0.0 115540   976 ?        S    16:17   0:00 /bin/bash /etc/rc.d/init.d/ceshiqi start
root       2056  0.0  0.0 112808   964 pts/0    S+   16:17   0:00 grep --color=auto ceshi
[root@centos7 ~]# tail -f /root/1.txt
1
2
3
4
............

生成进程名sh /root/script3.sh
[root@centos7 ~]# cat /etc/rc.d/init.d/ceshiqi 
#! /bin/bash
#
#
# chkconfig: 2345 40 30
# description:this is a test
#
# Source function library.
. /etc/init.d/functions

case "$1" in 
start)
sh /root/ceshi.sh  &    #注意注意:此处若不用sh运行脚本,而利用/root/ceshi.sh &。运行ceshiqi脚本后出现进程名为/etc/rc.d/init.d/ceshiqi start甚至找不到进程名的情况而导致stop参数无法杀死此进程。
;;
stop)
ps aux|grep ceshi.sh|grep -v grep|awk '{print $2}'|xargs kill -9  &>/dev/null             #进程名/root/ceshi.sh
;;
reload|restart)
ps aux|grep ceshi.sh|grep -v grep|awk '{print $2}'|xargs kill -9  &>/dev/null
sh /root/ceshi.sh &
;;
*) 
echo "usage:ceshiqi restart|start|stop"
;;
esac
exit 0
[root@centos7 ~]# chmod +x /etc/rc.d/init.d/ceshiqi
[root@centos7 ~]# cat /root/ceshi.sh 
count=1
for value in `seq 1000`
do
echo $[ $value + 1 ]
sleep 10
done >/root/11.txt
[root@centos7 ~]# ps aux|grep -e ceshiqi -e /root/ceshi.sh
root       3058  0.0  0.0 112812  1000 pts/0    S+   16:26   0:00 grep --color=auto -e ceshiqi -e /root/ceshi.sh
[root@centos7 ~]# service ceshiqi start
Starting ceshiqi (via systemctl):                          [  OK  ]
[root@centos7 ~]# ps aux|grep -e ceshiqi -e /root/ceshi.sh
root       3085  0.0  0.0 115412  1476 ?        S    16:26   0:00 sh /root/ceshi.sh
root       3089  0.0  0.0 112812  1004 pts/0    S+   16:26   0:00 grep --color=auto -e ceshiqi -e /root/ceshi.sh
[root@centos7 ~]# tail -f /root/11.txt
2
3
.......
^C
[root@centos7 ~]# service ceshiqi stop
Stopping ceshiqi (via systemctl):                          [  OK  ]
[root@centos7 ~]# ps aux|grep -e ceshiqi -e /root/ceshi.sh
root       3136  0.0  0.0 112812  1004 pts/0    S+   16:27   0:00 grep --color=auto -e ceshiqi -e /root/ceshi.sh
[root@centos7 ~]# service ceshiqi restart
Restarting ceshiqi (via systemctl):                        [  OK  ]
[root@centos7 ~]# ps aux|grep -e ceshiqi -e /root/ceshi.sh
root       3160  0.0  0.0 115412  1476 ?        S    16:27   0:00 sh /root/ceshi.sh
root       3164  0.0  0.0 112812  1004 pts/0    S+   16:27   0:00 grep --color=auto -e ceshiqi -e /root/ceshi.sh
[root@centos7 ~]# tail -f /root/11.txt
2
3
4
.......

/etc/rc.d/init.d目录下启用守护进程脚本

守护进程脚本文件/root/supervisor.sh编写:
[root@centos7 ~]# cat /root/supervisor.sh 
#!/bin/bash
#function supervisor process
LOG_FILE=/var/log/supervisor_sh.log

# log function
function log() {
   local t=$(date +"%F %X")
    echo "[ $t ] $0 : $1 " >> ${LOG_FILE}
}

# check process number
# $1 : process name
function check_process() {
    if [ -z $1 ]; then
        log "Input parameter is empty."
        return 0
    fi

    p_num=$(ps -ef | grep "$1" | grep -v "grep" | wc -l)
    log "p_num = $p_num"
    echo $p_num
}

# supervisor process
while [ 1 ]
do
    declare -i ch_num
    p_name="ceshi.sh"
    ch_num=$(check_process $p_name)
    if [ $ch_num -eq 0 ]; then
       ps -ef | grep "ceshi.sh" | grep -v "grep" |awk '{print $2}'|xargs kill -9
       sh /root/ceshi.sh &
    fi
    sleep 3
done
[root@centos7 ~]# cat /root/ceshi.sh
count=1
for value in `seq 1000`
do
echo $[ $value + 1 ]
sleep 10
done >/root/11.txt
[root@centos7 ~]# ps -ef|grep -e supervisor -e ceshi.sh
[root@centos7 ~]# sh /root/supervisor.sh &        #命令行运行脚本
...........
[root@centos7 ~]# ps -ef|grep -e supervisor -e ceshi.sh
root       9390   8645  0 14:22 pts/1    00:00:00 sh supervisor.sh
root       9405   9390  0 14:22 pts/1    00:00:00 sh /root/ceshi.sh
root      10567   8645  0 14:28 pts/1    00:00:00 grep --color=auto -e supervisor -e ceshi.sh
[root@centos7 ~]# kill -9 9390
[root@centos7 ~]# ps -ef|grep -e supervisor -e ceshi.sh
root       9405      1  0 14:22 pts/1    00:00:00 sh /root/ceshi.sh     #注意退出守护进程后/root/ceshi.sh的ppid,父进程变成1
root      10634   8645  0 14:29 pts/1    00:00:00 grep --color=auto -e supervisor -e ceshi.sh
[1]+  Killed                  sh supervisor.sh
# 

守护进程脚本文件/root/supervisor.sh加入/etc/rc.d/init.d/processdaemon内
[root@centos7 ~]# cat /etc/rc.d/init.d/processdaemon 
#!/bin/bash

# chkconfig: 35 41 31     #35:在3级别和5级别启动processdaemon;41:启动顺序为41号,S41;31:关闭顺序为31号,K31;当进行chkconfig --add processdaemon操作时,如果没有指定level那么就会来这个注释中取值
# description:this is process-daemon
#
# Source function library.
. /etc/init.d/functions        ####必须添加

case "$1" in 
start)
sh /root/supervisor.sh & >/dev/null 2>&1
;;
stop)
ps aux|grep supervisor.sh|grep -v grep|awk '{print $2}'|xargs kill -9  &>/dev/null
;;
reload|restart)
ps aux|grep supervisor.sh|grep -v grep|awk '{print $2}'|xargs kill -9  &>/dev/null
sh /root/supervisor.sh & >/dev/null 2>&1
;;
*) 
echo "usage:${0} restart|start|stop"
;;
esac
[root@centos7 ~]# chmod +x /etc/rc.d/init.d/processdaemon
[root@centos7 ~]# ps -ef|grep -e supervisor -e ceshi.sh
root       4285   1978  0 17:09 pts/0    00:00:00 grep --color=auto -e supervisor -e ceshi.sh
[root@centos7 ~]# service processdaemon start
Starting processdaemon (via systemctl):                    [  OK  ]
[root@centos7 ~]# ps -ef|grep -e supervisor -e ceshi.sh
root       4318      1  0 17:09 ?        00:00:00 sh /root/supervisor.sh
root       4333   4318  0 17:09 ?        00:00:00 sh /root/ceshi.sh
root       4339   1978  0 17:09 pts/0    00:00:00 grep --color=auto -e supervisor -e ceshi.sh
[root@centos7 ~]# service processdaemon restart
Restarting processdaemon (via systemctl):                  [  OK  ]
[root@centos7 ~]# ps -ef|grep -e supervisor -e ceshi.sh
root       4333      1  0 17:09 ?        00:00:00 sh /root/ceshi.sh
root       4410      1  0 17:09 ?        00:00:00 sh /root/supervisor.sh
root       4421   1978  0 17:09 pts/0    00:00:00 grep --color=auto -e supervisor -e ceshi.sh
[root@centos7 ~]# kill -9 4333
[root@centos7 ~]# ps -ef|grep -e supervisor -e ceshi.sh             #停止/root/ceshi.sh后进程被重新拉起
root       4410      1  0 17:09 ?        00:00:00 sh /root/supervisor.sh
root       4543   4410  0 17:09 ?        00:00:00 sh /root/ceshi.sh
root       4548   1978  0 17:10 pts/0    00:00:00 grep --color=auto -e supervisor -e ceshi.sh
[root@centos7 ~]# service processdaemon stop
Stopping processdaemon (via systemctl):                    [  OK  ]
[root@centos7 ~]# ps -ef|grep -e supervisor -e ceshi.sh
root       4543      1  0 17:09 ?        00:00:00 sh /root/ceshi.sh
root       4614   1978  0 17:10 pts/0    00:00:00 grep --color=auto -e supervisor -e ceshi.sh
[root@centos7 ~]# 

以函数方式添加守护;(建议用上面的方法将守护程序放到单独的脚本中)
[root@centos7 ~]# cat /etc/rc.d/init.d/processdaemon 
#!/bin/bash

# chkconfig: 35 41 31     #345:在3级别4级别和5级别启动ceshiqi;40:启动顺序为40号,S40;30:关闭顺序为30号,K30;当进行chkconfig --add ceshiqi操作时,如果没有指定level那么就会来这个注释中取值
# description:this is process-daemon
#
# Source function library.
. /etc/init.d/functions        ####必须添加

function supervisor() {
#function supervisor process
LOG_FILE=/var/log/supervisor_sh.log

# log function
function log() {
   local t=$(date +"%F %X")
    echo "[ $t ] $0 : $1 " >> ${LOG_FILE}
}

# check process number
# $1 : process name
function check_process() {
    if [ -z $1 ]; then
        log "Input parameter is empty."
        return 0
    fi

    p_num=$(ps -ef | grep "$1" | grep -v "grep" | wc -l)
    log "p_num = $p_num"
    echo $p_num
}

# supervisor process
while [ 1 ]
do
    declare -i ch_num
    p_name="ceshi.sh"
    ch_num=$(check_process $p_name)
    if [ $ch_num -eq 0 ]; then
       ps -ef | grep "ceshi.sh" | grep -v "grep" |awk '{print $2}'|xargs kill -9
       sh /root/ceshi.sh &
    fi
    sleep 3
done
}
#
case "$1" in 
start)
supervisor &
;;
stop)
ps aux|grep processdaemon|grep -v grep|awk '{print $2}'|xargs kill -9  &>/dev/null
;;
reload|restart)
ps aux|grep processdaemon|grep -v grep|awk '{print $2}'|xargs kill -9  &>/dev/null
supervisor &
;;
*) 
echo "usage:${0} restart|start|stop"
;;
esac
[root@centos7 ~]# cat /root/ceshi.sh
count=1
for value in `seq 1000`
do
echo $[ $value + 1 ]
sleep 10
done >/root/11.txt
[root@centos7 ~]# chmod +x /etc/rc.d/init.d/processdaemon
[root@centos7 ~]# systemctl daemon-reload
[root@centos7 ~]# service processdaemon start
Starting processdaemon (via systemctl):                    [  OK  ]
[root@centos7 ~]# ps -ef|grep -e processdaemon  -e ceshi.sh 
root       2683      1  0 18:19 ?        00:00:00 /bin/bash /etc/rc.d/init.d/processdaemon start
root       2698   2683  0 18:19 ?        00:00:00 sh /root/ceshi.sh
root       2865   2169  0 18:20 pts/0    00:00:00 grep --color=auto -e processdaemon -e ceshi.sh
[root@centos7 ~]# tail -f /var/log/supervisor_sh.log 
[ 2023-09-20 06:21:09 PM ] /etc/rc.d/init.d/processdaemon : p_num = 1 
[ 2023-09-20 06:21:12 PM ] /etc/rc.d/init.d/processdaemon : p_num = 1 
[ 2023-09-20 06:21:15 PM ] /etc/rc.d/init.d/processdaemon : p_num = 1 
[ 2023-09-20 06:21:18 PM ] /etc/rc.d/init.d/processdaemon : p_num = 1 
[ 2023-09-20 06:21:21 PM ] /etc/rc.d/init.d/processdaemon : p_num = 1 
[ 2023-09-20 06:21:24 PM ] /etc/rc.d/init.d/processdaemon : p_num = 1 
......
[root@centos7 ~]# tail -f /root/11.txt
60
61
62
.........

参考文章:https://blog.csdn.net/lcy4599/article/details/52267517
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值