def rename_job(args): """rename a job in user's crontab""" if "quiet" in args: Utils.DEBUG = not args.quiet count = 0 old_name = str(args.old_name) new_name = str(args.new_name) if Utils.check_job_name(new_name) == -1: Utils.debug_print("Error: job name cannot be '{}'.".format(name)) sys.exit(1) if Utils.check_job_name(new_name) == -2: Utils.debug_print("Error: job name cannot contain a '%' symbol.") sys.exit(1) cron = TCronTab(user=True) for job in cron: if job.get_name() == old_name: job.set_name(new_name) count += 1 if count: cron.activate_triggered_jobs(old_name, "deleted") cron.activate_triggered_jobs(new_name, "added") cron.write_to_user(user=True) if count == 0: Utils.debug_print("Error: job '{}' does not exist.".format(old_name)) elif count == 1: Utils.debug_print("1 job has been renamed from '{}' to '{}'." .format(old_name, new_name)) else: Utils.debug_print("{} jobs have been renamed from '{}' to '{}'." .format(count, old_name, new_name))
def add_job(args): """add the job to crontab""" if "quiet" in args: Utils.DEBUG = not args.quiet name = str(args.name) if Utils.check_job_name(name) == -1: Utils.debug_print("Error: job name cannot be '{}'.".format(name)) sys.exit(1) if Utils.check_job_name(name) == -2: Utils.debug_print("Error: job name cannot contain a '%' symbol.") sys.exit(1) command = str(args.command[0]) repetition = str(args.repetition[0]) repeat = Repetition.parse_repetition(repetition) if not repeat: Utils.debug_print("Error: invalid repetition sentence: '{}'." .format(repetition)) sys.exit(1) cron = TCronTab(user=True) job = cron.new(command=command, comment=name) if "reboot" in repeat: job.every_reboot() else: if "min_every" in repeat: job.minute.every(repeat['min_every']) if "min_on" in repeat: job.minute.on(repeat['min_on']) if "hour_every" in repeat: job.hour.every(repeat['hour_every']) if "hour_on" in repeat: job.hour.on(repeat['hour_on']) if "day_every" in repeat: job.day.every(repeat['day_every']) if "day_on" in repeat: job.day.on(repeat['day_on']) if "dow_on" in repeat: job.dow.on(*repeat['dow_on']) if "dow_during" in repeat: job.dow.during(*repeat['dow_during']) if "month_every" in repeat: job.month.every(repeat['month_every']) if "month_during" in repeat: job.month.during(*repeat['month_during']) if "month_on" in repeat: job.month.on(*repeat['month_on']) job.enable() cron.activate_triggered_jobs(name, "added") cron.write_to_user(user=True) Utils.debug_print("Job named '{}' has been successfully added.".format(name))
def delete_job(args): """delete the specified job from user's crontab""" if "quiet" in args: Utils.DEBUG = not args.quiet name = str(args.name) count = 0 cron = TCronTab(user=True) for job in cron: if job.get_name() == name: cron.remove(job) count += 1 if count: cron.activate_triggered_jobs(name, "deleted") cron.write_to_user(user=True) if count == 1: Utils.debug_print("1 job named '{}' has been deleted.".format(name)) else: Utils.debug_print("{} jobs named '{}' have been deleted.".format(count, name))
def _generic_enable_job(name, enable_it, quiet=None): """enable or disable job(s) by their name""" if quiet != None: Utils.DEBUG = not quiet count = 0 cron = TCronTab(user=True) for job in cron: if job.get_name() == name and job.is_enabled() != enable_it: job.enable(enable_it) count += 1 if enable_it: action = "enabled" if count: cron.activate_triggered_jobs(name, "enabled") else: action = "disabled" if count: cron.activate_triggered_jobs(name, "disabled") cron.activate_triggered_jobs(name, "toggled") cron.write_to_user(user=True) if count == 1: Utils.debug_print("1 job named '{}' has been {}.".format(name, action)) else: Utils.debug_print("{} jobs named '{}' have been {}.".format(count, name, action))