[英]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.