如何在远程服务器上启动进程,断开连接,然后收集输出? [英] How to start a process on a remote server, disconnect, then later collect output?

查看:61
本文介绍了如何在远程服务器上启动进程,断开连接,然后收集输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 python 编写自动化代码来测试网络应用程序的行为.因此,我的代码需要能够在网络中的服务器上启动进程/脚本(例如,tcpdump 或 python 脚本),断开连接,运行其他进程,然后返回并关闭/评估之前启动的进程.我的网络是 windows 和 linux 机器的混合,所有机器都运行 sshd 和 python(通过 Cygwin 用于 windows 机器).

I am writing automation code in python to test the behavior of a network application. As such, my code needs to be able to start a process/script (say, tcpdump or a python script) on a server in the network, disconnect, run other processes and then later return and shutdown/evaluate the process started earlier. My network is a mix of windows and linux machines and all of the machines have sshd and python running (via Cygwin for the windows machines).

我考虑了几个想法,即:- 启动一个进程并通过尾随符号 (&) 将其移至后台- 以某种方式使用屏幕- 使用python线程

I've considered a couple of ideas, namely: - Starting a process and moving it to the background via a trailing ampersand (&) - Using screen in some fashion - Using python threads

我还应该考虑什么?根据您的经验,您认为完成此类任务的最佳方式是什么?

What else should I be considering? In your experience what have you found to be the best way to accomplish a task like this?

推荐答案

既然我明白你真正想要什么,我认为最好的解决方案是创建一个 lite 守护进程来运行你想要的任何进程.然后守护程序可以将输出转储到日志文件中,因此如果运行时间比您预期的要短,您仍然可以获得输出.这比摆弄屏幕更可靠.然而,应该指出的是,这可能是一个潜在的安全风险.如果这是你每 N 个时间段运行一次的任务,cron 会工作得更好.

So now that I understand what you really want, I think the best solution would be to create a lite daemon to run whatever process you want. The daemon could then dump the output to a log file so if it takes a shorter amount of time to run than you expect, you can still get the output. This would be more reliable than fiddling with screen. It should be pointed out however, that this could potentially be a security risk. If this is a task you run every N time periods, cron would work much better.

原答案

GNU Screen 是处理此问题的好方法.

GNU Screen is a good way to handle this.

重新阅读您的问题后,我想我误解了您的意思.您是否希望在本地机器上运行一个 python 脚本,该脚本将连接到远程机器并运行一些任意进程,然后能够再次连接到远程机器并检查日志或其他内容?

After re-reading your question, I think I misunderstood you. Are you looking to run a python script on the local machine that will connect to a remote one and run some arbitrary process and then be able to connect to the remote machine again and examine a log or something?

这篇关于如何在远程服务器上启动进程,断开连接,然后收集输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆