Iterate through all jobs, this includes disabled (commented out) cron jobs: for job in cron: Iter = cron.find_comment(re.compile(' or \w'))įind an existing job by schedule: iter = cron.find_time(2, 10, '2-4', '*/2', None) Iter = cron.find_command(re.compile(r'br$'))įind an existing job by comment exact match or regular expression: iter = cron.find_comment('ID or some text') Job.set_comment("New ID or comment here")įind an existing job by command sub-match or regular expression: iter = cron.find_command('bar') # matches foobar1 Modify the comment or command on a job: job.set_command("new_script.sh") Get the comment or command for a job: command = job.command Job = cron.new('/bin/d', before=cron.find_command('/usr/bin/existing')) Job = cron.new('/bin/c', before=re.compile('id*')) Running the job here will not effect it’sĮxisting schedule with another crontab process: job_standard_output = job.run()Ĭreating a job with a comment: job = cron.new(command='/foo/bar', comment='SomeID')Ĭreating a job in the middle of the crontab: job = cron.new('/bin/a', before='someID') ![]() ![]() Setting the slice to a python date object: job.setall(time(10, 2)) Setting all time slices at once: job.setall(2, 10, '2-4', '*/2', None) System_cron.new(command='new_command', user='root')Ĭreating a new job is as simple as: job = cron.new(command='/usr/bin/echo')Īnd setting the job’s time restrictions: (5,50).every(5)Įach time restriction will clear the previous restriction: (10) # Set to * */10 * * *Īppending restrictions is explicit: (10) # Set to * */10 * * * Special per-command user flag for vixie cron format (new in 1.9): system_cron = CronTab(tabfile='/etc/crontab', user=False) Will work only on Unix and require you to have the right permissions: from crontab import CronTabĪnd two ways from non-system sources that will work on Windows too: file_cron = CronTab(tabfile='filename.tab') Getting access to a crontab can happen in five ways, three system methods that See below for full details on the use of the write function. You MUST use write() if you want your edits to be saved out. Note: Several users have reported their new crontabs not saving automatically or that the module doesn’t do anything. Job = cron.new(command='echo hello_world')Īlternatively, you can use the with context manager which will automaticallyĬall write on the cron object upon exit: with CronTab(user='root') as cron: Lastly,ĭeclared changes get written to the crontab by calling write on the object: from crontab import CronTab To declaratively manipulate the cron (spawning a new job in this case). First theĬronTab class is used to instantiate a cron object, then the cron object is used Here is a simple example of how python-crontab is typically used. These are the supported aliases which are not available in SystemV * * * 0 * * 0 * * 0 1 * 0 1 1 0 1 1 0 * * * ![]() Supported special cases allow crontab lines to not use fields. Standard Linux or SystemV crontab format.Įxtra Values are ‘’ for maximum value, such as 23 for hours or 12 for months. You will note that W, L, # and ? symbols are not supported as they are not DescriptionĬrontab module for reading and writing crontab files and accessing the system cronĪutomatically and simply using a direct API. You need to install python-crontab and not crontab from pypi or your local package manager and try again. Note: If you get the error got an unexpected keyword argument 'user' when using CronTab, you have the wrong module installed. Please use Git and push patches to the GitLab project code hosting. Please report any problems to the GitLab issues tracker.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |