栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

systemctl停止服务,同进程名的其它服务也被停止

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

systemctl停止服务,同进程名的其它服务也被停止

问题描述:

在/etc/init.d创建3个相同的服务,mytestservice1、mytestservice2、mytestservice3,其内容基本相同,mytestservice1如下代码:

#!/bin/bash
# chkconfig:1234 90 60
# description: Test for stopping service.

# mytestservice1、mytestservice2、mytestservice3只有这里不同
servername=myservice1
serverdir=/home/myservice1
binpath=/home/myservice1/myservice.sh
#servername=myservice2
#serverdir=/home/myservice2
#binpath=/home/myservice2/myservice.sh
#servername=myservice3
#serverdir=/home/myservice3
#binpath=/home/myservice3/myservice.sh

prog=$(basename $binpath)
. /etc/init.d/functions

restart() {
        stop
        start
}
reload() {
        stop
        start
}
start() {
	echo -n $"Starting $daemon:"
        daemon nohup $binpath &
        retval=$?
        echo
        [ $retval -eq 0 ]
}

stop() {
	echo -n $"Stopping $daemon:"
        killproc $binpath
        retval=$?
        echo
        [ $retval -eq 0 ]
}

ha_status() {
        status $prog
        ps -ef|grep $prog && exit 0
}

case "$1" in

     start)
        $1
     ;;
     stop)
        $1
     ;;
     reload)
        $1
     ;;
     restart)
        $1
     ;;
     status)
        ha_status
     ;;
     *)
        echo "Usage:$0 {start|stop|reload|restart|status}"
        exit 1
esac

启动mytestservice1、mytestservice2、mytestservice3

假设/home/myservice3/myservice.sh意外停止(这里用kill掉代替)

如果此时用 service mytestservice3 stop 停止服务,那么/home/myservice1/myservice.sh和/home/myservice2/myservice.sh会被停止。


原因分析:

分析service脚本、mytestservice脚本、systemctl机制,service或是systemctl最后都是通过/etc/rc.d/init.d/mytestservice3 stop停止服务,/etc/rc.d/init.d/mytestservice3是从/etc/init.d/mytestservice3拷贝的。

mytestservice3调用/etc/init.d/function中killproc函数停止服务,其中查找相应进程PID的命令,由于/home/myservice3/myservice.sh已经停止,会执行 “pidof -c -m -o $$ -o $PPID -o %PPID -x “myservice.sh””,因此会把myservice2和myservice3的myservice.sh的PID也搜索出来,Kill掉查找到的PID。

# Output PIDs of matching processes, found using pidof
__pids_pidof() {
    pidof -c -m -o $$ -o $PPID -o %PPID -x "$1" || 
        pidof -c -m -o $$ -o $PPID -o %PPID -x "${1##*/}"
}


解决方案:

改变各进程名称,让进程名各不相同。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/450658.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号