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.
pip install python-crontabVerified import paths — ran on the pinned version, not inferred.
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.
Migrate code to use `from crontab import CronTab` and adjust constructor arguments (e.g., `CronTab(user=True)`, `CronTab(user='root')`, `CronTab(tabfile='/etc/crontab')`). `CrontabEntry` objects are now `CronItem` objects within `CronTab` instances.
Always ensure `my_cron.write()` is called after making any additions, modifications, or removals of cron jobs to persist the changes.
Run your Python script with `sudo` if you intend to modify crontabs other than the current user's default. Be cautious when granting elevated privileges.
After modifying any attributes of a `CronItem` obtained from `my_cron`, ensure `my_cron.write()` is called to apply the changes.
pip install python-crontab
Iterate directly over the CronTab object: `for job in my_crontab:` or convert to a list: `list_of_jobs = list(my_crontab)`.
Run the script with appropriate elevated privileges (e.g., `sudo python your_script.py`) or ensure you have write access to the target crontab file.
Use `tabfile` instead of `tab` when initializing `CronTab`: `my_crontab = CronTab(tabfile='/path/to/crontab')`
No dependency data recorded yet.