Skip to main content

Python Crontab API

Project description

Description

Crontab module for read and writing crontab files and accessing the system cron automatically and simply using a direct API.

Comparing the below chart to http://en.wikipedia.org/wiki/Cron#CRON_expression you will note that W, L, # and ? symbols are not supported.

Field Name

Mandatory

Allowed Values

Allowed Special Characters

Minutes

Yes

0-59

* / , -

Hours

Yes

0-23

* / , -

Day of month

Yes

1-31

* / , -

Month

Yes

1-12 or JAN-DEC

* / , -

Day of week

Yes

0-6 or SUN-SAT

* / , -

Supported special cases allow crontab lines to not use fields. These are the supported aliases:

Case

Meaning

@reboot

Every boot

@hourly

0 * * * *

@daily

0 0 * * *

@weekly

0 0 * * 0

@monthly

0 0 1 * *

@yearly

0 0 1 1 *

@annually

0 0 1 1 *

@midnight

0 0 * * *

How to Use the Module

Getting access to a crontab can happen in four ways:

from crontab import CronTab

system_cron = CronTab()
user_cron = CronTab('root')
file_cron = CronTab(tabfile='filename.tab')
mem_cron = CronTab(tab="""
  * * * * * command
""")

Creating a new job is as simple as:

job  = cron.new(command='/usr/bin/echo')

And setting the job’s time restrictions:

job.minute.during(5,50).every(5)
job.hour.every(4)

job.dow.on('SUN')
job.month.during('APR', 'NOV')

Creating a job with a comment:

job = cron.new(command='/foo/bar',comment='SomeID')

Use a special syntax:

job.every_reboot()

Find an existing job by command:

list = cron.find_command('bar')

Clean a job of all rules:

job.clear()

Iterate through all jobs:

for job in cron:
    print job

Iterate through all lines:

for line in cron.lines:
    print job

Remove Items:

cron.remove( job )
cron.remove_all('echo')

Write CronTab back to system or filename:

cron.write()

Write CronTab to new filename:

cron.write( 'output.tab' )

Extra Support

  • Support for SunOS with compatability mode

  • Python 3.2 and Python 2.7 tested

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python-crontab-1.3.tar.gz (19.9 kB view details)

Uploaded Source

File details

Details for the file python-crontab-1.3.tar.gz.

File metadata

  • Download URL: python-crontab-1.3.tar.gz
  • Upload date:
  • Size: 19.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for python-crontab-1.3.tar.gz
Algorithm Hash digest
SHA256 16c9ab26eb669c6bc0afe2500539dd10d1aa4c7fdb0aad8fecfdb9acf04e3d86
MD5 235643f745666dfd54390ab3c7db8e97
BLAKE2b-256 800c6d1cb0cfad10b4f8dd9cfa07031b7fb9c3e97165c3bfb5400f53f7e7f747

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page