質問: syslog (Linux サーバー) で特定のイベントの最初のエントリを検索し、コマンドを実行して (将来の開発)、情報をログに記録しようとしています。これに正しい方法でアプローチしたかどうかはわかりません。
問題: 最初に見つかった有効なエントリでループを終了する方法がわかりません。
import os
import commands
from time import strftime
file = "server.log.1" # sample log
nlogFile =("/home/nsoper/log/logfile_%s.txt") %(strftime("%Y_%m_%d_ %H_%M_%S")) # create logfile
caption="SN" # sample serach
condition2 = "java error"
F=open(file, 'r')
nF=open(nlogFile, 'a')
nF.write("output for results search:\n\n")
print(strftime("%Y_%m_%d_ %H_%M_%S"))
for line in F:
for item in line.split("\n"):
if caption in item:
line1=item.strip()
if condition2 in line1:
print line1.strip()
nF.write(line1.strip()+"\n")
#command execute
nF.close()
F.close()