{"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":"en","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,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}