Menu

Post image 1
Post image 2
1 / 2
0

Python Task Scheduler: Run Any Script Automatically (No Cron Needed)

DEV Community·Brad·18 days ago
#e0GBj7px
Reading 0:00
15s threshold

Python Task Scheduler: Run Any Script Automatically (No Cron Needed) Cron is powerful but cryptic. Here's how to build a Python-based task scheduler that's easier to configure and more powerful than cron. Simple Task Scheduler with schedule Library # pip install schedule import schedule import time import threading from datetime import datetime from typing import Callable class TaskScheduler : def __init__ ( self ): self . jobs = [] self . running = False def add_job ( self , func : Callable , interval : str , * args , ** kwargs ): """ Add a job to the scheduler. interval examples: ' every 10 minutes ' , ' daily at 09:30 ' , ' every monday at 08:00 ' """ parts = interval . lower (). split () if parts [ 0 ] == ' every ' : if len ( parts ) == 2 and parts [ 1 ] == ' day ' : job = schedule . every (). day . do ( func , * args , ** kwargs ) elif len ( parts ) == 3 : amount = int ( parts [ 1 ]) unit = parts [ 2 ]. rstrip ( ' s ' ) # Remove plural job = getattr ( schedule . every ( amount ), unit ).…

Continue reading — create a free account

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

Read More