Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 以定时间隔将命令输出打印到文件_Python_Shell_Timer_Intervals_Timed - Fatal编程技术网

Python 以定时间隔将命令输出打印到文件

Python 以定时间隔将命令输出打印到文件,python,shell,timer,intervals,timed,Python,Shell,Timer,Intervals,Timed,我需要能够以定时间隔将命令输出打印到单个文件中。我正在使用的Python示例如下所示: import time i = 0 print('START!') while i >= 0: f = open('timelog.txt','a') nowstr = time.strftime('%Y-%m-%d %H:%M:%S') print(nowstr) f.write(nowstr) f.write('\n'

我需要能够以定时间隔将命令输出打印到单个文件中。我正在使用的Python示例如下所示:

import time
i = 0
print('START!')

while i >= 0:
        f = open('timelog.txt','a')
        nowstr = time.strftime('%Y-%m-%d %H:%M:%S')
        print(nowstr)
        f.write(nowstr)
        f.write('\n')
        f.close()
        time.sleep(60)
        i = i + 1

print('DONE!');
上面的代码执行它需要的操作,每60秒在新行上将日期和时间打印到单个文件中(不覆盖现有条目或日期)。我需要修改代码,以在固定的时间间隔执行shell命令(目标是以定时的时间间隔将shell命令输出记录到文件中)


谢谢。

“以时间为单位执行shell命令”?请解释。您可以尝试执行命令或尝试库,这是一个围绕子流程的方便包装器。