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))