Created
October 19, 2016 13:41
-
-
Save jcrsilva/381b342aad6b347a21c2343ef9ab79f9 to your computer and use it in GitHub Desktop.
Simple python scheduler script with logging
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import datetime | |
import logging | |
from logging.handlers import RotatingFileHandler | |
import os | |
import subprocess | |
import sys | |
import threading | |
iterations = 0 | |
run = True | |
env = os.environ.copy() # copy env variables to be passed to the child process | |
interval = 5 # mins | |
# __file__ gets unset on the second thread run | |
command = 'python {} {}'.format(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'script.py'), ' '.join(sys.argv[1:])) | |
def scheduled_task(): | |
global iterations, run, command, env, interval | |
iterations += 1 | |
logging.info(u'[{}] Running iteration number {}\n'.format(datetime.datetime.now(), iterations)) | |
try: | |
process = subprocess.Popen( | |
command, | |
shell=True, | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE, | |
env=env | |
) | |
output, unused_err = process.communicate() | |
retcode = process.poll() | |
if retcode: | |
logging.critical("Command '{}' failed with error code {} and message:\n {}".format(command, retcode, output)) | |
raise subprocess.CalledProcessError(retcode, cmd=command, output=output) | |
logging.info(output) | |
# schedule the next run | |
if run: | |
logging.info("Scheduling another run for {} mins in time".format(interval)) | |
threading.Timer(interval * 60, scheduled_task).start() | |
except Exception as e: | |
run = False | |
logging.error(str(e)) | |
raise | |
def setup_logging(): | |
log_formatter = logging.Formatter("%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s] %(message)s") | |
root_logger = logging.getLogger() | |
console_handler = logging.StreamHandler(sys.stdout) | |
console_handler.setFormatter(log_formatter) | |
root_logger.addHandler(console_handler) | |
if os.path.isdir(os.path.join(os.getcwd(), 'logs')): | |
file_handler = RotatingFileHandler(os.path.join(os.getcwd(), 'logs', 'autoagent.log'), | |
maxBytes=10000, | |
backupCount=10 | |
) | |
file_handler.setFormatter(log_formatter) | |
root_logger.addHandler(file_handler) | |
root_logger.setLevel(logging.INFO) | |
setup_logging() | |
scheduled_task() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment