kafka-server-stop.sh不工作

k2arahey  于 2021-06-07  发布在  Kafka
关注(0)|答案(5)|浏览(349)

在远程节点上部署了一些apachekafka示例之后,我发现了这个问题 kafka-server-stop.sh Kafka档案的一部分。
默认情况下,它包含:


# !/bin/sh

# Licensed to the Apache Software Foundation (ASF) under one or more

# contributor license agreements.  See the NOTICE file distributed with

# this work for additional information regarding copyright ownership.

# The ASF licenses this file to You under the Apache License, Version 2.0

# (the "License"); you may not use this file except in compliance with

# the License.  You may obtain a copy of the License at

# 

# http://www.apache.org/licenses/LICENSE-2.0

# 

# Unless required by applicable law or agreed to in writing, software

# distributed under the License is distributed on an "AS IS" BASIS,

# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

# See the License for the specific language governing permissions and

# limitations under the License.

ps ax | grep -i 'kafka\.Kafka' | grep java | grep -v grep | awk '{print $1}' | xargs kill -SIGTERM

如果我将apache kafka作为非后台进程来执行,那么这个脚本非常有用,例如:

/var/lib/kafka/bin/kafka-server-start.sh /var/lib/kafka/config/server.properties

当我将其作为后台进程执行时,它也可以工作:

/var/lib/kafka/bin/kafka-server-start.sh /var/lib/kafka/config/server.properties &

但是在我的远程节点上,我用这个python脚本执行它(使用ansible):


# !/usr/bin/env python

import argparse
import os
import subprocess

KAFKA_PATH = "/var/lib/kafka/"

def execute_command_pipe_output(command_to_call):
  return subprocess.Popen(command_to_call, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

def execute_command_no_output(command_to_call):
  with open(os.devnull, "w") as null_file:
    return subprocess.Popen(command_to_call, stdout=null_file, stderr=subprocess.STDOUT)  

def start_kafka(args):
  command_to_call = ["nohup"]
  command_to_call += [KAFKA_PATH + "bin/zookeeper-server-start.sh"]
  command_to_call += [KAFKA_PATH + "config/zookeeper.properties"]

  proc = execute_command_no_output(command_to_call)

  command_to_call = ["nohup"]
  command_to_call += [KAFKA_PATH + "bin/kafka-server-start.sh"]
  command_to_call += [KAFKA_PATH + "config/server.properties"]

  proc = execute_command_no_output(command_to_call)

def stop_kafka(args):
  command_to_call = [KAFKA_PATH + "bin/kafka-server-stop.sh"]

  proc = execute_command_pipe_output(command_to_call)
  for line in iter(proc.stdout.readline, b''):
    print line,

  command_to_call = [KAFKA_PATH + "bin/zookeeper-server-stop.sh"]

  proc = execute_command_pipe_output(command_to_call)
  for line in iter(proc.stdout.readline, b''):
    print line,

if __name__ == "__main__":
  parser = argparse.ArgumentParser(description="Starting Zookeeper and Kafka instances")
  parser.add_argument('action', choices=['start', 'stop'], help="action to take")

  args = parser.parse_args()

  if args.action == 'start':
    start_kafka(args)
  elif args.action == 'stop':
    stop_kafka(args)
  else:
    parser.print_help()

执行后

manage-kafka.py start
manage-kafka.py stop

zookeeper关闭了(应该是这样),但kafka仍在运行。
更有趣的是,当我(用手)调用

nohup /var/lib/kafka/bin/kafka-server-stop.sh

或者

nohup /var/lib/kafka/bin/kafka-server-stop.sh &
``` `kafka-server-stop.sh` 正确关闭Kafka示例。我怀疑这个问题可能是由linux/python引起的。
cvxl0en2

cvxl0en21#

在想出解决问题的办法之前,我曾多次面对这个问题。所以发生的事情是Kafka突然关闭,但港口仍在使用中。
按照以下步骤操作:
查找在该端口上运行的进程的进程id: lsof -t -i :YOUR_PORT_NUMBER . ##这是给麦克的
结束这个过程 kill -9 process_id

kgsdhlau

kgsdhlau2#

更改中的命令 kafka-server-stop.sh 为了解决我的问题:

PIDS=$(ps axww | grep -i 'kafka\.Kafka' | grep java | grep -v grep | nawk '{print $1}')

说明:
问题是 kafka-server-stop.sh 使用以下命令获取要终止的PID:

PIDS=$(ps ax | grep -i 'kafka\.Kafka' | grep java | grep -v grep | awk '{print $1}')

终端上的“ps”80列问题:
问题是 ps ax 没有显示命令的所有输出,因为它被截断为xx列(通常为80列,以前的默认终端宽度)。我的是168列,定义见 stty -a . 正在更改为 ps axww 是的,简言之,这扩大了产出。
awk输入记录长度问题:
另一个问题是awk有一个 Characters per input record limitation of 3000 chars 如本文所述。 nawk 相反地,不存在也受到价值的限制 C long . gawk 也可以。
这样做的缺点是,我正在修改一个核心脚本,在升级过程中可能会被覆盖。它很快,而且可能很脏,但它对我来说很管用。
p、 如果你有兴趣的话,我在这里找到了一个吉拉。

iyfjxgzm

iyfjxgzm3#

我猜:kafka-server-stop.sh使用shell管道。所以波本需要 shell=True 争论。
看到了吗https://docs.python.org/2/library/subprocess.html#subprocess.popen

eblbsuwk

eblbsuwk4#

在执行kafka-zookeeper-stop.sh管理工具之前,请先练习kafka-server-stop.sh。它将首先断开服务器与zookeeper的连接,然后停止zookeeper本身。请等待3-4秒,然后再开始。

bis0qfac

bis0qfac5#

Kafka需要在关闭前完成关闭过程。
因此,启动zookeers,然后代理将重试关闭过程。
我也遇到过类似的情况。问题是我的配置没有等待Kafka代理关闭。希望这能帮助别人。我花了一段时间才明白。。。

相关问题