API
You can use crondir from your Python code
from crondir import Crondir
crondir = Crondir(cron_dir="~/override_path/")
print("Existing crontab:", crondir.read())
print("Adding a snippet file")
crondir.add_file(source_file, snippet="snippet1", force=True)
print("Adding a snippet string")
crondir.add_string(
"0 * * * * /home/deploy/project/bin/hourly_refresh.sh",
snippet="snippet2",
force=True,
)
print("Removing files")
crondir.remove("snippet1", force=True)
crondir.remove("snippet2", force=True)
print("Building")
crondir.build()
API reference
- class crondir.crondir.Crondir(cron_dir: pathlib.Path | str | None = None)
Bases:
object- add_file(source_file: pathlib.Path | str, *, snippet: str | None = None, force: bool = False) Path
Add a cron snippet file to the cron dir
- Parameters
source_file (Path | str) – The path of the cron definition to add.
force (bool) – If False, raise an error if the destination file exists.
snippet (str | None) – The name for the file in the cron_dir. If not set, use the current filename.
- add_string(*contents: str, snippet: str | None, force: bool = False) Path
Add a cron snippet string to the cron dir under the specified name
- backup(backup_path: pathlib.Path | str | None)
Backup the current crontab to the backup dir
Does not backup if the crontab is empty
- Parameters
backup_path (Path | str | None) – The path of the backup dir. If None, use the env var CRONDIR_BACKUP, or the default backup dir.
- build() str
Build the crontab from the source dir
- Returns
The new crontab
- Return type
str
- list() list[pathlib.Path]
List the snippets in the cron dir
- read(refresh: bool = False, check: bool = False) str
Read the crontab
- Parameters
refresh (bool) – If True, ignore any cached value
check (bool) – If True, raise a CrontabFail error, otherwise return empty
- Returns
Current contents of the crontab
- Return type
str
- remove(snippet: str, *, force: bool = False) bool
Remove a cron file from the cron dir
- Returns
True if a snippet was removed, False if it didn’t exist
- Return type
bool