Hello Members,
I am working on an automation wherein I need to monitor a location on cloud and if there is a new file my Python program is suppose to do some activity on it and save the latest timestamp in a variable.
I have written a code which is doing its job perfectly. Since I have used a 'while' loop so it is supposed to run in infinite loop. But there has some unexpected scenario occurred and now I need to execute my program through a cron job. So, obviously I cannot use the 'while' loop anymore. Instead I need to save the latest timestamp in a file and re-reread every-time the cron job is executed. Below is my code snippet
But I am not sure how to,or where to use them so that during every cron cycle the file is read to compare the timestamp of the latest file and the timestamp stored in the file.
Please let me know if my explanation is not clear.
Thank you,
I am working on an automation wherein I need to monitor a location on cloud and if there is a new file my Python program is suppose to do some activity on it and save the latest timestamp in a variable.
I have written a code which is doing its job perfectly. Since I have used a 'while' loop so it is supposed to run in infinite loop. But there has some unexpected scenario occurred and now I need to execute my program through a cron job. So, obviously I cannot use the 'while' loop anymore. Instead I need to save the latest timestamp in a file and re-reread every-time the cron job is executed. Below is my code snippet
import glob
import logging
import os
import subprocess
import time
logger = logging.getLogger("")
logger.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s:%(levelname)s:%(name)s:%(message)s")
file_handler = logging.FileHandler("/var/log/ab.log")
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
comp_timestamp = None
while True:
source_files = list(glob.iglob("<path>/*.deb"))
if source_files:
latest_file = max(source_files, key=os.path.getmtime)
latest_timestamp = os.path.getmtime(latest_file)
if (comp_timestamp is None) or (latest_timestamp > comp_timestamp):
try:
logging.info("copying latest file '{}'..".format(latest_file))
subprocess.Popen(["cp", "-r", latest_file, "/tmp"])
logging.info("waiting for 5 seconds...")
time.sleep(5)
comp_timestamp = latest_timestamp
os.chdir("/tmp")
deb_files = list(glob.iglob("/tmp/*.deb"))
if deb_files:
local_file = max(deb_files, key=os.path.getmtime)
local_timestamp = os.path.getmtime(local_file)
logging.info("depackaging local debian file '{}'..".format(local_file))
subproc = subprocess.Popen(["dpkg", "--install", local_file])
logging.info(subproc.__dict__)
time.sleep(5)
else:
logging.error("Latest file not found")
except AttributeError as att:
logging.exception(latest_file)
else:
logging.info("No newer files found.")
else:
logging.warning("No files found at all.")
time.sleep(1)I can use the python file handling as shown below to write and then read the file. Please correct me if I am wrong:with open('test.txt', 'w') as tp:
tp.write('my name is khan')
with open('test.txt') as re:
data = re.read() But I am not sure how to,or where to use them so that during every cron cycle the file is read to compare the timestamp of the latest file and the timestamp stored in the file.
Please let me know if my explanation is not clear.
Thank you,
