I want to read the last three lines from the kernel log /var/log/messages
in a loop.
for i in xrange(0,100):
# do_stuff()
file = open('/var/log/messages')
lines = file.readlines()[-3:]
# analyse_stuff()
file.close()
But I have a problem with this code:
[...]
1013477960613797 1013477960959759 1013477961174602
1013477960613797 1013477960959759 1013477961174602
1013477960613797 1013477960959759 1013477961174602
1013477960613797 1013477960959759 1013477961174602
1013477973551967 1013477973773506 1013477977678559
1013477973551967 1013477973773506 1013477977678559
1013477973551967 1013477973773506 1013477977678559
1013477973551967 1013477973773506 1013477977678559
1013477973551967 1013477973773506 1013477977678559
1013477973551967 1013477973773506 1013477977678559
1013477973551967 1013477973773506 1013477977678559
1013477973551967 1013477973773506 1013477977678559
1013477973551967 1013477973773506 1013477977678559
1013477973551967 1013477973773506 1013477977678559
1013477973551967 1013477973773506 1013477977678559
1013477973551967 1013477973773506 1013477977678559
1013477973551967 1013477973773506 1013477977678559
1013477973551967 1013477973773506 1013477977678559
1013477973551967 1013477973773506 1013477977678559
1013477973551967 1013477973773506 1013477977678559
1013477973551967 1013477973773506 1013477977678559
1013477973551967 1013477973773506 1013477977678559
1013477973551967 1013477973773506 1013477977678559
1013477973551967 1013477973773506 1013477977678559
1013477973551967 1013477973773506 1013477977678559
1013477973551967 1013477973773506 1013477977678559
1013477973551967 1013477973773506 1013477977678559
1013477986756370 1013477990527612 1013477990834895
1013477986756370 1013477990527612 1013477990834895
1013477986756370 1013477990527612 1013477990834895
1013477986756370 1013477990527612 1013477990834895
1013477986756370 1013477990527612 1013477990834895
1013477986756370 1013477990527612 1013477990834895
1013477986756370 1013477990527612 1013477990834895
1013477986756370 1013477990527612 1013477990834895
[...]
As you can see, values are repeated many times.
do_stuff()
executes a binary, which issues three log messages.
analyse_stuff()
will analyse the three lines and does magic
I always close the file and reopen it, because I thought this would resolve the problem, but it doesn't. Any idea what happens?