问题描述:
在/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##*/}"
}
解决方案:
改变各进程名称,让进程名各不相同。