Question: I'm trying to search a syslog (Linux server) for the first entry of an specific event and then execute a command (future development) and then log the information. I'm not sure if I have approached this the right way.
Problem: not sure how to exit the loop on the first valid entry found.
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()