编写一个 Python 守护进程来管理多个远程尾部?
在上一个线程的帮助下,我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论