我编写了一个正在流动的python脚本;
以上所有内容都保存在日志文件中,一切正常,但我没有得到我想要的日志文件。
import subprocess
from datetime import datetime
from sys import stderr
logfile="/var/tmp/home-backup.log"
TimeStamp = datetime.now().strftime("%a %d %b %Y %H:%M:%S - ")
BackStamp = datetime.now().strftime("%d-%b-%Y")
Portal = "192.168.1.59:3260"
Target = "iqn.2014-07.com.synology:fabdskst.linux-tgt1.888dddfd71"
File_obj = open(logfile, "a",0)
MyCommand = "sudo iscsiadm -m node --targetname " + Target + " --portal \"" + Portal + "\"" + " --login"
Discover = "sudo iscsiadm -d 0 -m discovery -t sendtargets -p 192.168.1.59:3260"
MyMount = "sudo mount -t ext4 -v /dev/sdc /media/linuxLUN01/"
MyBackup = "tar -cvzf /media/linuxLUN01/Mint_backup_" + BackStamp + ".tgz /home/fabrice/"
File_obj.write(TimeStamp)
subprocess.call(MyCommand, shell=True, stdout=File_obj, stderr=subprocess.PIPE)
File_obj.write(TimeStamp)
subprocess.call(MyMount, shell=True, stdout=File_obj, stderr=subprocess.PIPE)
File_obj.write(TimeStamp)
subprocess.call(MyBackup, shell=True, stdout=File_obj, stderr=subprocess.PIPE)
File_obj.close()
我希望stdout中的每一行都有时间标记输出到屏幕的实时(在我的情况下,stdout被重定向到日志文件)。相反,只有命令的第一个输出带有时间戳,其余的不是。
我正在学习python所以不是我不好(你可能已经注意到我现在不做任何测试,这将在稍后进行)。
我正在考虑如何解决这个问题,但我只想出一个非常优雅的解决方案,在时间方面并不严格;
任何有其他想法可以让这更准确的人?理想情况下,我想拦截每个stdout条目,并在它出来时添加时间戳。目前备份大约需要5分钟。
任何真正意义上的建议
根据Ram的要求,从日志文件中提取
Sat 04 Oct 2014 22:15:04 - Logging in to [iface: default, target: iqn.2014-07.com.synology:fabdskst.linux-tgt1.888dddfd71, portal: 192.168.1.59,3260] (multiple)
Login to [iface: default, target: iqn.2014-07.com.synology:fabdskst.linux-tgt1.888dddfd71, portal: 192.168.1.59,3260] successful.
Sat 04 Oct 2014 22:15:04 - Sat 04 Oct 2014 22:15:04 - /home/fabrice/
/home/fabrice/.gnome2_private/
/home/fabrice/.profile
/home/fabrice/.ICEauthority
/home/fabrice/.linuxmint/
/home/fabrice/.linuxmint/mintUpload/
/home/fabrice/.linuxmint/mintUpload/services/
/home/fabrice/Downloads/
/home/fabrice/Downloads/mshell.zip
/home/fabrice/Downloads/bash-4.3.tar.gz
/home/fabrice/Downloads/VMwareTools-9.2.4-1398046.tar.gz
/home/fabrice/Downloads/vmware-tools-distrib/
/home/fabrice/Downloads/vmware-tools-distrib/vmware-install.pl
/home/fabrice/Downloads/vmware-tools-distrib/lib/
/home/fabrice/Downloads/vmware-tools-distrib/lib/lib32/
/home/fabrice/Downloads/vmware-tools-distrib/lib/lib32/libgdkmm-2.4.so.1/
/home/fabrice/Downloads/vmware-tools-distrib/lib/lib32/libgdkmm-2.4.so.1/libgdkmm-2.4.so.1
您可以看到每个命令的输出的第一行都有时间戳
至于stderr = subprocess.PIPE没有特别的理由我使用它我只是把它放在我在网上找到的例子上,我仍然需要了解它的作用。
答案 0 :(得分:0)
不是经过测试的解决方案,但我相信它运行良好:
import subprocess as sub
[...]
with open(logfile, "a", 0) as File_obj:
for cmd in (MyCommand, MyMount, MyBackup):
with sub.Popen(cmd, shell=True, stdout=sub.PIPE, universal_newlines=True) as proc:
while proc.poll() is None:
log = [TimeStamp + line for line in proc.stdout.readlines()]
File_obj.writelines(log)
Obs。:您希望stderr输出到终端,对吗?