简体   繁体   English

有没有办法使用python将终端命令的输出存储到文件中?

[英]Is there a way I can store the output of a terminal command into a file using python?

I want to store the output of the terminal command top into a file, using Python. 我想使用Python将终端命令top的输出存储到一个文件中。 In the terminal, when I type top and hit enter, I get an output that is real time, so it keeps updating. 在终端中,当我键入top并按回车键时,我得到的是实时输出,因此它会不断更新。 I want to store this into a file for a fixed duration and then stop writing. 我想将其存储到文件中固定的时间,然后停止写入。

file=open("data.txt","w")
file.flush()
import os,time
os.system("top>>data.txt -n 1")
time.sleep(5)
exit()
file.close()

I have tried to use time.sleep() and then exit() , but it doesn't work, and the only way top can be stopped is in the terminal, by Control + C The process keeps running and the data is continuously written onto the file, which is not ideal, as one would guess 我尝试过使用time.sleep()然后exit() ,但是它不起作用,并且可以停止top的唯一方法是通过Control + C在终端中,该过程保持运行,并且数据被连续写入到文件上,这是不理想的,就像人们猜到的那样

For clarity: I know how to write the output on to the file, I just want to stop writing after a period 为了清楚起见:我知道如何将输出写入文件,我只是想在一段时间后停止写入

system will wait for the end of the child process. system将等待子进程的结束。 If you do not want that, the Pythonic way is to directly use the subprocess module: 如果您不希望这样做,Pythonic方法是直接使用子流程模块:

import subprocess

timeout=60   # let top run for one minute
file=open("data.txt","w")
top = subprocess.Popen(["top", "-n", 1], stdout=file)
if top.wait(timeout) is None:      # wait at most timeout seconds
    top.terminate()                # and terminate child

The panonoic way (which is highly recommended for robust code) would be to use the full path of top . Panonoic方法(强烈建议使用健壮的代码)将使用top的完整路径。 I have not here, because it may depend on the actual system... 我不在这里,因为它可能取决于实际系统...

The issue you could be facing is that os.system starts the process as part of the current process. 您可能面临的问题是os.system作为当前进程的一部分启动该进程。 So the rest of your script will not be run until the command you run has completed execution. 因此,在您运行的命令完成执行之前,脚本的其余部分将不会运行。

I think what you want to be doing is executing your console command on another thread so that the thread running your python script can continue while the command runs in the background. 我认为您要执行的操作是在另一个线程上执行控制台命令,以使运行python脚本的线程可以在命令在后台运行时继续运行。 See run a python program on a new thread for more info. 有关更多信息,请参见在新线程上运行python程序

I'd suggest something like (this is untested): 我建议类似的东西(未经测试):

import os
import time
import multiprocessing

myThread = multiprocessing.process(target=os.system,  args=("top>>data.txt -n 1",))
myThread.start()

time.sleep(5)
myThread.terminate()

That being said, you may need to consider the thread safety of os.system(), if it is not thread safe you'll need to find an alternative that is. 话虽如此,您可能需要考虑os.system()的线程安全性,如果它不是线程安全的,则需要找到一个替代方法。

Something else worth noting (and that I know little about) is that it may not be ideal to terminate threads in this way, see some of the answers here: Is there any way to kill a Thread? 还有一点值得注意(我对此知之甚少)是,以这种方式终止线程可能不是理想的选择,请参见此处的一些答案: 有没有办法杀死线程?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将使用SSH从Python脚本运行的命令的输出存储到文件中? - How can I store the output of a command I ran using SSH from a Python script into a file? 我无法运行 python 的任何命令或文件 - 终端中没有 output - I am unable to run any command or file of python - there is no output in terminal 我可以用Python中的一个命令写入终端和给定文件吗? - Can I write to terminal and a given file with one command in Python? Python命令输出终端 - Python Command Output Terminal 如何使用 Python 异步运行终端命令? - How can I run a terminal command asynchronously using Python? 如何使用 Python 写入文本文件,以便我可以在终端/gnuplot 中同时读取它 - How to write to a text file using Python such a way that I can read it simultaneously in the terminal/gnuplot 如何将打印在终端控制台上的 output 存储到 Python 中的文本文件中 - How to store output printed on terminal console to a text file in Python 文件和终端上的 Python 输出 - Python output on file and terminal 使用命令提示符时如何将 Python 脚本输出保存到文件? - How can I Save a Python Script output to a file when using Command Prompt? 禁止Python命令的终端输出 - Suppress the terminal output of a Python command
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM