Registry / workflow / python-crontab

python-crontab

JSON →
library3.3.0pypypi✓ verified 35d ago

python-crontab provides a Pythonic API to create, manage, read, and write crontab entries and entire crontab files, including user-specific and system-wide crontabs. As of version 3.3.0, it offers a unified `CronTab` class for all crontab manipulations, simplifying interaction with cron jobs from Python. The library is actively maintained with an irregular release cadence.

workflowdevops
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

The top-level module is `crontab`, not `python_crontab` matching the package name.

from crontab import CronTab

The main class is `CronTab` (capital 'T'), not `Crontab` (lowercase 't'), especially since version 3.0.0.

from crontab import CronTab

This example initializes a crontab for the current user, adds a new job to run every minute, and then persists the changes to the user's crontab file. Remember that `write()` is crucial to save modifications. This requires the Python script to have appropriate permissions to modify the crontab.

from crontab import CronTab # Initialize crontab for the current user. Permissions might be needed. # For system-wide crontabs, use CronTab(user='root') and ensure proper permissions. my_cron = CronTab(user=True) # Create a new cron job job = my_cron.new(command='echo "Hello from cron!" >> /tmp/cron_test.log', comment='my_test_job') job.minute().every(1) # Run every minute # Iterate existing jobs (optional) print('Existing jobs:') for j in my_cron: print(j) # IMPORTANT: Write changes to the crontab file my_cron.write() print('Crontab updated. Check /tmp/cron_test.log in a minute.') # To remove a job later (example): # for job_to_remove in my_cron.find_comment('my_test_job'): # my_cron.remove(job_to_remove) # my_cron.write()
crontab --version
Debug
Known footguns
breakingVersion 3.0.0 introduced significant breaking changes. The `Crontab` (system-wide) and `CrontabEntry` classes were removed/renamed. All crontab manipulations now happen through the unified `CronTab` class, with parameters like `user` or `tabfile` to specify the target crontab.
gotchaChanges made to the `CronTab` object are not persisted to the actual crontab file until the `.write()` method is explicitly called. Forgetting to call `.write()` will result in no actual changes to the system's cron jobs.
gotchaRunning `python-crontab` to modify system or other user's crontabs often requires elevated privileges (e.g., `sudo`). Without proper permissions, operations like `my_cron.write()` might fail silently or raise permission errors.
gotchaWhen finding jobs, methods like `find_command`, `find_comment`, or iteration (`for job in my_cron:`) return `CronItem` objects. Modifying these `CronItem` objects directly requires `my_cron.write()` to save the changes.
Upgrade
Version history

Breaking-change detection hasn't run for this library yet.

Audit
Security & dependencies

CVE tracking and dependency tree are planned for a later release.

Agent activity
0 hits · last 30 days

No traffic data recorded yet.

Resources