我想要一个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>
警告/错误/输出吗?关于如何修复它们的任何建议?提前致谢!