Menu

1 / 2

Python Task Scheduler: Run Any Script Automatically at Any Time

DEV Community·Brad·6 months ago
#JM8ONjdn
Reading 0:00
15s threshold

Python Task Scheduler: Run Any Script Automatically at Any Time

The Problem

Running scripts manually is error-prone and easy to forget. Production systems need reliable scheduling.

The Solution

Build a robust task scheduler using Python's schedule library with error handling and logging.

Complete Implementation

import schedule import time import logging from datetime import datetime

logging . basicConfig ( level = logging . INFO , format = " %(asctime)s %(levelname)s %(message)s " )

def run_task ( name , func ): """ Wrapper that logs execution and catches errors. """ def wrapper (): logging . info ( f " Starting task: { name } " ) try : func () logging . info ( f " Completed task: { name } " ) except Exception as e : logging . error ( f " Task { name } failed: { e } " ) return wrapper

def backup_database (): import shutil shutil . copy ( " db.sqlite " , f " backup_ { datetime .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.