编写一个 Python 守护进程来管理多个远程尾部?

发布于 2024-12-09 07:49:59 字数 1609 浏览 2 评论 0原文

在上一个线程的帮助下,我正在使用 Python 和 Paramiko 在远程服务器上的一大堆日志文件上运行 tail -f

Paramiko 和 exec_command - 杀死远程进程?

我的最终脚本看起来像这样:

#!/usr/bin/env python2
import paramiko
import select
import random

tail_id = random.randint(0,500)
username = 'victorhooi'

client = paramiko.SSHClient()
client.load_system_host_keys()
client.connect('someserver.com', username='victorhooi', password='somepassword')
transport = client.get_transport()
#transport.set_keepalive(1)
channel = transport.open_session()

channel.exec_command("tail -%df /home/victorhooi/macbeth.txt" % tail_id)
while True:
    try:
        rl, wl, xl = select.select([channel],[],[],0.0)
        if len(rl) > 0:
            # Must be stdout
            print channel.recv(1024).strip()
    except KeyboardInterrupt:
        print("Caught control-C")
        client.get_transport().open_session().exec_command("kill -9 `ps -fu %s | grep 'tail -%df /home/victorhooi/macbeth.txt' | grep -v grep | awk '{print $2}'`" % (username, tail_id))
        #channel.close()
        #transport.close()
        client.close()
        exit(0)

我现在需要扩展它来处理后台处理,管理多个尾部,然后根据需要杀死特定的尾部。

理想情况下,我有一个可以启动并后台运行的 Python 脚本(或者守护进程? python-daemon?)。然后,该进程可以读取配置文件,并启动单独的 paramiko 调用来跟踪每个远程日志文件。

我还有一个控制脚本,我可以运行它来列出正在运行的远程尾部,并杀死特定的尾部,或者杀死所有尾部,或者停止/重新启动守护进程。

解决这个问题有什么好办法吗?我应该使用线程或多处理还是其他方式来实现每个 Paramiko 调用?是否有任何现有的脚本/程序可供我查看如何执行此操作的示例?

让管理脚本与每个进程/线程进行通信的好方法是什么?

干杯, 胜利者

I'm using Python and Paramiko to run tail -f on a whole bunch logfiles on remote servers, with help from a previous thread:

Paramiko and exec_command - killing remote process?

My final script looks something like this:

#!/usr/bin/env python2
import paramiko
import select
import random

tail_id = random.randint(0,500)
username = 'victorhooi'

client = paramiko.SSHClient()
client.load_system_host_keys()
client.connect('someserver.com', username='victorhooi', password='somepassword')
transport = client.get_transport()
#transport.set_keepalive(1)
channel = transport.open_session()

channel.exec_command("tail -%df /home/victorhooi/macbeth.txt" % tail_id)
while True:
    try:
        rl, wl, xl = select.select([channel],[],[],0.0)
        if len(rl) > 0:
            # Must be stdout
            print channel.recv(1024).strip()
    except KeyboardInterrupt:
        print("Caught control-C")
        client.get_transport().open_session().exec_command("kill -9 `ps -fu %s | grep 'tail -%df /home/victorhooi/macbeth.txt' | grep -v grep | awk '{print $2}'`" % (username, tail_id))
        #channel.close()
        #transport.close()
        client.close()
        exit(0)

I now need to extend this to handle being backgrounded, and managing multiple tails, and then killing specific tails on demand.

Ideally, I'd have one Python script that I could spin up and background itself (Or daemonize? python-daemon?). This process could then read in a configuration file, and start up separate paramiko calls to tail each remote logfile.

I'd also have a control script that I could run to list the remote tails that were running, and kill specific ones, or kill all of them, or stop/restart the daemon.

What's a good way of tackling this problem? Should I be using threading or multiprocessing, or something else to achieve each Paramiko call? Are there any existing scripts/programs that I can look to for an example of how to do this?

And what's a good way of having the management script communicate with each of the processes/threads?

Cheers,
Victor

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文