gpt4 book ai didi

python - 万无一失的跨平台进程kill daemon

转载 作者:IT王子 更新时间:2023-10-29 00:14:45 27 4
gpt4 key购买 nike

我有一些 python 自动化,它生成了我使用 linux script 记录的 telnet session 命令;有两个script每个日志记录 session 的进程 ID(父进程和子进程)。

我需要解决一个问题,如果 python 自动化脚本死了,script session 永远不会自行关闭;由于某种原因,这比它应该做的要难得多。

到目前为止,我已经实现了 watchdog.py(请参阅问题的底部),它 self 守护,并在循环中轮询 python 自动化脚本的 PID。当它看到 python 自动化 PID 从服务器的进程表中消失时,它会尝试终止 script。 session 。

我的问题是:

  • script session 总是产生两个独立的进程,其中一个是 script sessions 是另一个的父级 script session 。
  • watchdog.py 不会杀死 child script session ,如果我开始script来自自动化脚本的 session (参见下面的自动化示例)

自动化示例(reproduce_bug.py)

import pexpect as px
from subprocess import Popen
import code
import time
import sys
import os

def read_pid_and_telnet(_child, addr):
time.sleep(0.1) # Give the OS time to write the PIDFILE
# Read the PID in the PIDFILE
fh = open('PIDFILE', 'r')
pid = int(''.join(fh.readlines()))
fh.close()
time.sleep(0.1)
# Clean up the PIDFILE
os.remove('PIDFILE')
_child.expect(['#', '\$'], timeout=3)
_child.sendline('telnet %s' % addr)
return str(pid)

pidlist = list()
child1 = px.spawn("""bash -c 'echo $$ > PIDFILE """
"""&& exec /usr/bin/script -f LOGFILE1.txt'""")
pidlist.append(read_pid_and_telnet(child1, '10.1.1.1'))

child2 = px.spawn("""bash -c 'echo $$ > PIDFILE """
"""&& exec /usr/bin/script -f LOGFILE2.txt'""")
pidlist.append(read_pid_and_telnet(child2, '10.1.1.2'))

cmd = "python watchdog.py -o %s -k %s" % (os.getpid(), ','.join(pidlist))
Popen(cmd.split(' '))
print "I started the watchdog with:\n %s" % cmd

time.sleep(0.5)
raise RuntimeError, "Simulated script crash. Note that script child sessions are hung"

现在是当我运行上面的自动化时发生的事情的例子...注意 PID 30017 产生 30018 和 PID 30020 产生 30021。所有上述 PID 都是 script session 。

[mpenning@Hotcoffee Network]$ python reproduce_bug.py 
I started the watchdog with:
python watchdog.py -o 30016 -k 30017,30020
Traceback (most recent call last):
File "reproduce_bug.py", line 35, in <module>
raise RuntimeError, "Simulated script crash. Note that script child sessions are hung"
RuntimeError: Simulated script crash. Note that script child sessions are hung
[mpenning@Hotcoffee Network]$

在我运行上面的自动化之后,所有的 child script session 仍在运行。

[mpenning@Hotcoffee Models]$ ps auxw | grep script
mpenning 30018 0.0 0.0 15832 508 ? S 12:08 0:00 /usr/bin/script -f LOGFILE1.txt
mpenning 30021 0.0 0.0 15832 516 ? S 12:08 0:00 /usr/bin/script -f LOGFILE2.txt
mpenning 30050 0.0 0.0 7548 880 pts/8 S+ 12:08 0:00 grep script
[mpenning@Hotcoffee Models]$

我在 Python 2.6.6 下运行自动化,在 Debian Squeeze Linux 系统上 (uname -a: Linux Hotcoffee 2.6.32-5-amd64 #1 SMP Mon Jan 16 16:22:28 UTC 2012 x86_64 GNU/Linux).

问题:

守护进程似乎无法在生成进程崩溃后幸存下来。如果自动化停止(如上例所示),我该如何修复 watchdog.py 以关闭所有脚本 session ?

说明问题的 watchdog.py 日志(遗憾的是,PID 与原始问题不一致)...

[mpenning@Hotcoffee ~]$ cat watchdog.log 
2012-02-22,15:17:20.356313 Start watchdog.watch_process
2012-02-22,15:17:20.356541 observe pid = 31339
2012-02-22,15:17:20.356643 kill pids = 31352,31356
2012-02-22,15:17:20.356730 seconds = 2
[mpenning@Hotcoffee ~]$

决议

问题本质上是竞争条件。当我试图杀死“父”script 进程时,它们已经与自动化事件同时死亡......

要解决这个问题...首先,看门狗守护进程需要在轮询观察到的 PID 之前识别要杀死的整个子列表(我的原始脚本试图在观察到的 PID 崩溃后识别 child )。接下来,我必须修改我的看门狗守护程序,以允许某些 script 进程可能会随着观察到的 PID 而终止。


看门狗.py:

#!/usr/bin/python
"""
Implement a cross-platform watchdog daemon, which observes a PID and kills
other PIDs if the observed PID dies.

Example:
--------

watchdog.py -o 29322 -k 29345,29346,29348 -s 2

The command checks PID 29322 every 2 seconds and kills PIDs 29345, 29346, 29348
and their children, if PID 29322 dies.

Requires:
----------

* https://github.com/giampaolo/psutil
* http://pypi.python.org/pypi/python-daemon
"""
from optparse import OptionParser
import datetime as dt
import signal
import daemon
import logging
import psutil
import time
import sys
import os

class MyFormatter(logging.Formatter):
converter=dt.datetime.fromtimestamp
def formatTime(self, record, datefmt=None):
ct = self.converter(record.created)
if datefmt:
s = ct.strftime(datefmt)
else:
t = ct.strftime("%Y-%m-%d %H:%M:%S")
s = "%s,%03d" % (t, record.msecs)
return s

def check_pid(pid):
""" Check For the existence of a unix / windows pid."""
try:
os.kill(pid, 0) # Kill 0 raises OSError, if pid isn't there...
except OSError:
return False
else:
return True

def kill_process(logger, pid):
try:
psu_proc = psutil.Process(pid)
except Exception, e:
logger.debug('Caught Exception ["%s"] while looking up PID %s' % (e, pid))
return False

logger.debug('Sending SIGTERM to %s' % repr(psu_proc))
psu_proc.send_signal(signal.SIGTERM)
psu_proc.wait(timeout=None)
return True

def watch_process(observe, kill, seconds=2):
"""Kill the process IDs listed in 'kill', when 'observe' dies."""
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logfile = logging.FileHandler('%s/watchdog.log' % os.getcwd())
logger.addHandler(logfile)
formatter = MyFormatter(fmt='%(asctime)s %(message)s',datefmt='%Y-%m-%d,%H:%M:%S.%f')
logfile.setFormatter(formatter)


logger.debug('Start watchdog.watch_process')
logger.debug(' observe pid = %s' % observe)
logger.debug(' kill pids = %s' % kill)
logger.debug(' seconds = %s' % seconds)
children = list()

# Get PIDs of all child processes...
for childpid in kill.split(','):
children.append(childpid)
p = psutil.Process(int(childpid))
for subpsu in p.get_children():
children.append(str(subpsu.pid))

# Poll observed PID...
while check_pid(int(observe)):
logger.debug('Poll PID: %s is alive.' % observe)
time.sleep(seconds)
logger.debug('Poll PID: %s is *dead*, starting kills of %s' % (observe, ', '.join(children)))

for pid in children:
# kill all child processes...
kill_process(logger, int(pid))
sys.exit(0) # Exit gracefully

def run(observe, kill, seconds):

with daemon.DaemonContext(detach_process=True,
stdout=sys.stdout,
working_directory=os.getcwd()):
watch_process(observe=observe, kill=kill, seconds=seconds)

if __name__=='__main__':
parser = OptionParser()
parser.add_option("-o", "--observe", dest="observe", type="int",
help="PID to be observed", metavar="INT")
parser.add_option("-k", "--kill", dest="kill",
help="Comma separated list of PIDs to be killed",
metavar="TEXT")
parser.add_option("-s", "--seconds", dest="seconds", default=2, type="int",
help="Seconds to wait between observations (default = 2)",
metavar="INT")
(options, args) = parser.parse_args()
run(options.observe, options.kill, options.seconds)

最佳答案

你的问题是 script 在生成后没有从自动化脚本中分离出来,所以它作为 child 工作,当 parent 去世时它仍然无法管理。

要处理 python 脚本退出,您可以使用 atexit模块。要监视子进程退出,您可以使用 os.wait或者处理 SIGCHLD 信号

关于python - 万无一失的跨平台进程kill daemon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9400724/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com