1

我想要一个grep用于特定进程的 Python 脚本,提取它的 PID,然后杀死它。这是我正在使用的:

def main():
    # Shutdown Tomcat
    shutdownCmd = "sh ${TOMCAT_HOME}/bin/shutdown.sh"
    subprocess.call([shutdownCmd], shell=True)

    # Check for PID
    grepCmd = "ps -aef | grep tomcat"
    grepResults = subprocess.check_output([grepCmd], shell=True)

    # Get PID(s) and kill it/them.
    for i in range(1, len(grepResults), 9):
    pid = grepResults[i]
    killPidCmd = "kill -9 " + pid
    subprocess.call([killPidCmd], shell=True)

    # Exit.
    sys.exit()

但是,尽管它确实关闭了我的进程(Apache Tomcat),但每当我运行它时,我都会在终端中收到以下“警告”:

kill -l [exitstatus]
/bin/sh: 1: kill: Operation not permitted

/bin/sh: 1: kill: Illegal number: h
/bin/sh: 1: kill: Illegal number: v
/bin/sh: 1: kill: Illegal number: l
/bin/sh: 1: kill: Operation not permitted

/bin/sh: 1: kill: Illegal number: .
/bin/sh: 1: kill: Illegal number: i
/bin/sh: 1: kill: Illegal number: D
/bin/sh: 1: kill: Illegal number: l
/bin/sh: 1: kill: Illegal number: .
/bin/sh: 1: kill: Illegal number: i
/bin/sh: 1: kill: Illegal number: /
/bin/sh: 1: kill: Illegal number: s
/bin/sh: 1: kill: Illegal number: r
/bin/sh: 1: kill: Illegal number: /
/bin/sh: 1: kill: Illegal number: .
/bin/sh: 1: kill: Illegal number: c
/bin/sh: 1: kill: Illegal number: t
/bin/sh: 1: kill: Illegal number: c
/bin/sh: 1: kill: Illegal number: i
/bin/sh: 1: kill: Illegal number: r
/bin/sh: 1: kill: Illegal number: a
/bin/sh: 1: kill: Illegal number: l
/bin/sh: 1: kill: Illegal number: a
/bin/sh: 1: kill: Illegal number: g
/bin/sh: 1: kill: Illegal number: j
/bin/sh: 1: kill: Illegal number: s
/bin/sh: 1: kill: Illegal number: g
/bin/sh: 1: kill: Illegal number: 
/bin/sh: 1: kill: Illegal number: X
/bin/sh: 1: kill: Illegal number: 
/bin/sh: 1: kill: Usage: kill [-s sigspec | -signum | -sigspec] [pid | job]... or
kill -l [exitstatus]
/bin/sh: 1: kill: Illegal number: n
/bin/sh: 1: kill: Illegal number: i
/bin/sh: 1: kill: Illegal number: /
/bin/sh: 1: kill: Illegal number: s
/bin/sh: 1: kill: Illegal number: r
/bin/sh: 1: kill: Illegal number: /
/bin/sh: 1: kill: Illegal number: .
/bin/sh: 1: kill: Illegal number: c
/bin/sh: 1: kill: Illegal number: t
/bin/sh: 1: kill: Illegal number: e
/bin/sh: 1: kill: Illegal number: 
/bin/sh: 1: kill: Illegal number: h
/bin/sh: 1: kill: Illegal number: h
/bin/sh: 1: kill: Illegal number: n
/bin/sh: 1: kill: Illegal number: /
/bin/sh: 1: kill: Illegal number: o
/bin/sh: 1: kill: Illegal number: .
/bin/sh: 1: kill: Illegal number: e
/bin/sh: 1: kill: Operation not permitted

/bin/sh: 1: kill: Illegal number: n
/bin/sh: 1: kill: Illegal number: a
/bin/sh: 1: kill: Illegal number: o
/bin/sh: 1: kill: Illegal number: e
/bin/sh: 1: kill: Illegal number: x
/bin/sh: 1: kill: Illegal number: c
/bin/sh: 1: kill: Illegal number: t
/bin/sh: 1: kill: Illegal number: a
/bin/sh: 1: kill: Illegal number: m
/bin/sh: 1: kill: Operation not permitted

/bin/sh: 1: kill: Illegal number: m
/bin/sh: 1: kill: Illegal number: .
/bin/sh: 1: kill: Illegal number: t
/bin/sh: 1: kill: Illegal number: s
/bin/sh: 1: kill: Illegal number: z
/bin/sh: 1: kill: Illegal number: a
/bin/sh: 1: kill: Illegal number: v
/bin/sh: 1: kill: Illegal number: t
Killed

任何 Python meastros 都可以查看脚本并弄清楚为什么我会收到所有这些/bin/sh: 1: kill: Illegal number: <whatever>警告/错误/输出吗?关于如何修复它们的任何建议?提前致谢!

4

3 回答 3

3
for i in range(1, len(grepResults), 9):
    pid = grepResults[i]
    killPidCmd = "kill -9 " + pid
    subprocess.call([killPidCmd], shell=True)

看起来您的 grepResults 是一些普通字符串,并且您正在从中选择单个字符。这就是为什么kill抱怨你给它的奇怪数字(不是数字而是字符)。检查此命令的输出,然后您应该看到出了什么问题:

grepResults = subprocess.check_output([grepCmd], shell=True)

插入类似的东西并检查是否pid适合您。还要确保条件 ( if 'grep' in line) 适合且不匹配其他流程。也许您必须在此处添加更多 if 子句。

import subprocess
grepCmd = "ps -aef | grep tomcat"
grepResults = subprocess.check_output([grepCmd], shell=True)
for line in grepResults.strip().split('\n'):
    if 'grep' in line: 
        continue
    pid = line.split()[1]
else:
    pid = None
# print pid

如果pidNone,它应该退出并出错,因为没有找到正在运行的服务。如果你想杀死多个pid,当然你必须创建一些列表,或者直接杀死这个循环中的 pid。

于 2012-10-02T11:02:21.140 回答
1

您可以尝试重定向stderr/dev/nullwith./script.py 2>/dev/null以消除这些消息。

于 2012-10-02T10:58:13.773 回答
1

问题是您将 grep 输出视为一个列表,而它实际上是一个字符串.. 更改您的行:

killPidCmd = "kill -9 " + pid

至:

killPidCmd = "echo kill -9 " + pid

..看看输出,问题就清楚了。

稍微调整的脚本版本,它不使用shell=True(很少需要,并导致 shell 转义问题),相当于 Python 中的 grep,

import os
import subprocess


def main():
    # Shutdown Tomcat
    scriptPath = "%s/bin/shutdown.sh" % (os.environ[TOMCAT_HOME])
    shutdownCmd = ["sh", scriptPath]
    subprocess.call(shutdownCmd)

    # Check for PID
    psCmd = ["ps", "-aef"]
    p = subprocess.Popen(psCmd, stdout=subprocess.PIPE)
    so, se = p.communicate()

    grep = [line for line in so.splitlines() if "python" in line]

    for line in grep:
        pid = line.split()[1] # Second column
        killPidCmd = ["kill", "-9", pid]
        subprocess.call(killPidCmd)

if __name__ == '__main__':
    main()

我再次建议您更改killPidCmd = ["kill", "-9", pid]killPidCmd = ["echo", "kill", "-9", pid]验证一切正常

也就是说,这作为一个 shell 脚本会简单得多:

${TOMCAT_HOME}/bin/shutdown.sh

pids=$(ps -aef | grep tomcat | awk '{print $2}')
kill -9 ${pids}
于 2012-10-02T11:30:04.203 回答