Log log file to avoid re-running
Simply write a file when it is running.
If the status in the file is 'running' skip the run
If any exception, write to log as well.
import os
import time
def read_log(path):
with open(path, 'r') as f:
return f.read()
def write_log(path, string):
with open(path, 'w') as f:
f.write(string)
log_file = 'c:\\temp\\test.log'
try:
if os.path.exists(log_file) and 'running' in read_log(log_file):
print('i am already running')
else:
write_log(log_file, 'Status: running')
#main job
for i in range(10):
print('doing sth {}'.format(i))
time.sleep(1)
# test exception
# if i == 3:
# 'abc'/2
print('job finished')
write_log(log_file, 'Status: finished')
except Exception as e:
print('finished with exception {}'.format(str(e)))
write_log(log_file, 'Exception: {}'.format(str(e)))