我是一个实习生,将Linux脚本移植到Python2.6。这个脚本基本上支持一个内部网仪表板来显示有关服务器的数据。它每分钟更新一次,并且一直在运行,几乎是24/7。
我希望得到一些帮助,将下面的Bash行转换为Python:
(while sleep 30; do custom_cmd > tmp.txt; cp tmp.txt index.html; rm tmp.txt; done) & 我对转换'&‘感到困惑,我知道它会将while循环转换为后台进程。while sleep 30无限地运行(只要用户处于活动状态),并且每30秒工作一次(在那之前睡觉)。我已经将custom_cmd (为仪表板生成html )移植到Python2.6。
Bash脚本配置为"nohup",我认为这意味着即使用户注销了Linux机器,该脚本也会运行。
话虽如此,我如何才能将上面的Bash行转换为Python,使它永远作为后台进程运行呢?非常感谢。
发布于 2020-01-17 16:03:59
可以这样编写简单的bash脚本:
from subprocess import call, PIPE
from tempfile import SpooledTemporaryFile
from time import sleep
def myfunc():
while True:
sleep(30)
handle = SpooledTemporaryFile()
if call(['custom_cmd'], stdout=handle) == 0:
handle.seek(0)
open('index.html', 'w').write(handle.read())
handle.close()然后,您应该使用去梦来守护函数。
https://stackoverflow.com/questions/59790172
复制相似问题