I am trying to get a python program to continuously run until a certain aws log is registered and printed. It is supposed to:
- Run indefinitely even if no events happen
- List whatever events occurs (in my case a log stating that the task is finished)
- Stop running
the python command looks like this: python3 watch_logs.py <log-source> --start=15:00:00
The logs are working fine, and the python script can print them out between certain time frames as long as they already exist. The program works by taking a continuously running task which prints events to the log file, and the python script should filter out events I am looking for and print them.
But, when I run the script, it wont print the event even if I can see the log entry appear in the file. If i kill the process and run it again using the same timestamp, it will find the log entry and end the script like it should.
the code is fairly short:
logs = get_log_events(
log_group=log_group,
start_time=start_time,
end_time=end_time
)
while True:
for event in logs:
print(event['message'].rstrip())
sys.exit("Task complete")
Any insight why this is happening would help a lot. I am fairly new to python
Orion :
The value in the variable logs is old when the file is updated. You need to update this variable. For example if you were to use logs = myfile.read() at start of your script, the value in the logs variable would be a snapshot of that file at that time.",
2019-08-07T14:44:58
PMM :
Try storing event['message'].rstrip() in a variable and checking with an if statement if it corresponds to the log you want to find",
2019-08-07T14:27:58
Hans-Martin Mosner :
If you don't want to read the file each time through the loop, you should have a look at pygtail (https://pypi.org/project/pygtail/).",
2019-08-07T14:47:51