From b28dfbc12e2197b2b46ba46729ae8b035021c524 Mon Sep 17 00:00:00 2001 From: leiva Date: Mon, 24 Mar 2025 13:57:45 +0100 Subject: [PATCH] ADD: Cron job arg creation --- src/adgroupsync/conf.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/adgroupsync/conf.py b/src/adgroupsync/conf.py index d6b5b58..20c3d76 100644 --- a/src/adgroupsync/conf.py +++ b/src/adgroupsync/conf.py @@ -84,6 +84,23 @@ def create_config_file(dest: Path): # Write configuration file TOMLFile(dest).write(conf) +def create_cron_job(cron_job: str, config_file: Path): + # Check if the script exists and its executable + if not shutil.which(__package__): + print( + f"No executable found, please add this to your crontab manually: '/path/to/adgroupsync --conf {config_file} >/dev/null 2>&1'" + ) + + # Checking if the string is valid + if not CronSlices.is_valid(cron_job): + raise Exception(f"Cron job '{cron_job}' is not valid.") + + # Creating the cron job + cron = CronTab(user=True) + job = cron.new(command=f"adgroupsync --conf {config_file} >/dev/null 2>&1") + job.setall(cron_job) + cron.write() + # Assign environment variables or configuration file values AD_DOMAIN = os.getenv("AD_DOMAIN") AD_USER_NAME = os.getenv("AD_USER")