{"id":1676,"library":"python-crontab","title":"Python Crontab API","description":"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.","status":"active","version":"3.3.0","language":"python","source_language":"en","source_url":"https://github.com/thomas-messier/python-crontab","tags":["cron","scheduler","system","automation","job-scheduling"],"install":[{"cmd":"pip install python-crontab","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"note":"The top-level module is `crontab`, not `python_crontab` matching the package name.","wrong":"from python_crontab import CronTab","symbol":"CronTab","correct":"from crontab import CronTab"},{"note":"The main class is `CronTab` (capital 'T'), not `Crontab` (lowercase 't'), especially since version 3.0.0.","wrong":"from crontab import Crontab","symbol":"CronTab","correct":"from crontab import CronTab"}],"quickstart":{"code":"from crontab import CronTab\n\n# Initialize crontab for the current user. Permissions might be needed.\n# For system-wide crontabs, use CronTab(user='root') and ensure proper permissions.\nmy_cron = CronTab(user=True)\n\n# Create a new cron job\njob = my_cron.new(command='echo \"Hello from cron!\" >> /tmp/cron_test.log', comment='my_test_job')\njob.minute().every(1) # Run every minute\n\n# Iterate existing jobs (optional)\nprint('Existing jobs:')\nfor j in my_cron:\n    print(j)\n\n# IMPORTANT: Write changes to the crontab file\nmy_cron.write()\nprint('Crontab updated. Check /tmp/cron_test.log in a minute.')\n\n# To remove a job later (example):\n# for job_to_remove in my_cron.find_comment('my_test_job'):\n#     my_cron.remove(job_to_remove)\n# my_cron.write()","lang":"python","description":"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."},"warnings":[{"fix":"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.","message":"Version 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.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Always ensure `my_cron.write()` is called after making any additions, modifications, or removals of cron jobs to persist the changes.","message":"Changes 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.","severity":"gotcha","affected_versions":"All"},{"fix":"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.","message":"Running `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.","severity":"gotcha","affected_versions":"All"},{"fix":"After modifying any attributes of a `CronItem` obtained from `my_cron`, ensure `my_cron.write()` is called to apply the changes.","message":"When 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.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"search_vec":"'3.3.0':35 'activ':56 'api':3,10 'autom':66 'cadenc':62 'class':41 'creat':12 'cron':49,63 'crontab':2,6,17,21,31,40,44 'entir':20 'entri':18 'file':22 'includ':23 'interact':47 'irregular':60 'job':50,68 'job-schedul':67 'librari':54 'maintain':57 'manag':13 'manipul':45 'offer':37 'provid':7 'python':1,5,9,52 'python-crontab':4 'read':14 'releas':61 'schedul':64,69 'simplifi':46 'specif':26 'system':29,65 'system-wid':28 'unifi':39 'user':25 'user-specif':24 'version':34 'wide':30 'write':16","created_at":"2026-04-09T03:58:21.334880+00:00","updated_at":"2026-04-16T20:17:49.283657+00:00","problems":[{"fix":"pip install python-crontab","cause":"The `python-crontab` package, which provides the `crontab` module, is not installed in the active Python environment.","error":"ModuleNotFoundError: No module named 'crontab'"},{"fix":"Iterate directly over the CronTab object: `for job in my_crontab:` or convert to a list: `list_of_jobs = list(my_crontab)`.","cause":"The `CronTab` object is directly iterable to access its jobs; it does not expose a public attribute named `jobs` (or `entries`) for this purpose.","error":"AttributeError: 'CronTab' object has no attribute 'jobs'"},{"fix":"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.","cause":"The Python script is attempting to write to a crontab file (e.g., another user's crontab or the system crontab) without the necessary operating system permissions.","error":"OSError: [Errno 13] Permission denied: '/var/spool/cron/username'"},{"fix":"Use `tabfile` instead of `tab` when initializing `CronTab`: `my_crontab = CronTab(tabfile='/path/to/crontab')`","cause":"The `CronTab` constructor expects `tabfile` as the keyword argument to specify the path to a crontab file, not `tab`.","error":"TypeError: CronTab.__init__() got an unexpected keyword argument 'tab'"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"3.3.0","cli_name":"crontab","cli_version":"sh: 1: crontab: not found","type":"library","homepage":"https://gitlab.com/doctormo/python-crontab/","github":null,"docs":null,"changelog":null,"pypi":"https://pypi.org/project/python-crontab/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["workflow","devops"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-08-27","next_check":"2026-07-28","install_tag":null}}