There are very few utility programs as useful as cron in all computer systems based on Unix. It’s designed is to schedule commands to run at a specific time. This type of scheduled command or task is called a "cron job".
Typically, cron
is used to run scheduled backups, monitor disk space, clean old logs files and residue periodically, perform system maintenance tasks, and a lot more. Developers can easily send commands directly to cron
to create and manage cron jobs, as it’s included in most Linux distro.
This article lists the best 5 mature and actively maintained Python package that can be used to work with cron jobs from inside your Python application.
Cronutils
cronutils is a Python package designed to simplify the configuration and dispatching of tasks via cron, as well as to give nicely formatted error reports by mail.
If what you want to do is simply creating and managing cron jobs, cronutils is the perfect choice. Some of its features are email error reporting (with the help of cronic), automatic process killing if a task takes longer than expected to run, error aggregation, Sentry and Django integration.
In order to use cronutils, Ubuntu/Debian users can install the dependencies using the following command.
sudo apt-get install sendmail mailutils moreutils
Install cronutils
pip install cronutils
Project Repository and Documentation : https://github.com/zagaran/cronutils
Python Crontab
Python-Crontab is a Python package that allows for reading and writing crontab configuration, i.e. cron jobs, programmatically using a direct API.
The package supports both Python 3 (3.5 to 3.7) and Python 2.7. Windows has limited support for only non-system crontabs. SystemV based platform is also supported, inclusing SunOS, AIX and HP.
Below is a simple example on how we would use Python-Crontab in a program.
from crontab import CronTab
cron = CronTab(user='root')
job = cron.new(command='echo hello_world')
job.minute.every(1)
cron.write()
Install Python-Crontab
pip install python-crontab
Project Repository and Documentation : https://gitlab.com/doctormo/python-crontab
django-cron
django-cron is a Python package developed by Tivix that lets you run Django/Python code periodically, providing basic plumbing to track and execute tasks.
The 2 most common ways to tackle the above problem is to either writing custom Python scripts or a management command per cron (leads to too many management commands!). Along with that some mechanism to track success, failure etc. is also usually necesary. django-cron app solves both issues to a reasonable extent.
Please note that django-cron is simply a Django application and cannot be used to replace full-brown queues solution like Celery or Python-RQ.
Install django-cron.
pip install django-cron
Project Repository and Documentation : https://github.com/tivix/django-cron
aiocron – Crontabs for asyncio
aiocron is a Python package that allows you to manage cron jobs in Python programs built using asyncio. aiocron relies on croniter to enable datetime object iteration, similar to what cron works.
There are two ways you can use aiocron programmatically, the first way is a decorator to run function at time,
@aiocron.crontab('*/30 * * * *')
async def attime():
print('run')
and the second way to use it is through await
keyword.
await crontab('0 * * * *').next()
Install aiocron:
pip install aiocron
Project Repository and Documentation : https://github.com/gawel/aiocron
nb_cron
nb_cron is a small Jupyter extension that allows you to managing cron jobs from within Jupyter. The extension adds a tab named Cron to the Jupyter file browsers. In that tab, you can view the list of the current cron jobs, edit or delete them or add a new one.
In order to install nb_cron, you have to install the actual package using pip
first, then add it to Jupyter using nbextension
and serverextension
command.
pip install nb_cron
jupyter nbextension install nb_cron --py --sys-prefix --symlink
jupyter nbextension enable nb_cron --py --sys-prefix
jupyter serverextension enable nb_cron --py --sys-prefix
Project Repository and Documentation : https://github.com/alexanghh/nb_cron