Skip to main content

Maintain a set of cron jobs in your code repository

Project description

cronrepo: Maintain a set of cron jobs in your code repository.

In Unix conventions, periodic tasks are invoked by cron jobs. These jobs are normally configured by the user interactively. When building a complex system that contains code that needs to be executed periodically, one usually needs to configure related cron jobs and to ensure that they are installed exactly at the moment when the repository gets deployed. This gets cumbersome and error-prone.

The cronrepo system eases that pain.

Cron job files

A directory should be created for the cron job files. They are normally shell scripts, although you can use other type of programs as well. They need to be line-based, though allowing a comment style headed by "#". So using Perl or Python scripts or even Makefile as such jobs are okay.

The cron job files will be tagged by "taglines" to tell what cron jobs should be installed on each target. The simplest ones look like this:

# CRON@alice::1-10/2 05 01-07 * 2,4

The above tagline configures a cron job running when the following criteria is matched:

  • minute: between 1 to 10, if divisible by 2.
  • hour: equals 5
  • day: between 01 and 07
  • month: any month
  • day of week: 2 (Tuesday) and 4 (Thursday)

So it is a job which runs 5 times on the first Tuesday and first Thursday of every month, at 05:02, 05:04, 05:06, 05:08 and 05:10.

Not all cron time formats are supported, and the above demonstrated all the supported types.

The "alice" is called the "target" of the cron job. When the cron jobs are installed on the system, one target is installed at a time. This allows you to have a cron directory containing jobs that runs differently on different targets, e.g., different machines.

Multiple taglines may be created for the same target. It is at times handy to be able to differentiate them. We can add a "job ID" to the above line, like this:

# CRON@alice%second::11-20/2 05 01-07 * 2,4 + foo bar

The job ID consists of word characters (letters, digits and underscores). The job ID is set as the environment variable CRONREPO_JID.

The above job shows one more feature of the tagline: we can add parameters to the cron job, by adding it to the tagline after a "+" character. The above job will be executed with two arguments "foo" and "bar".

The cronrepo program

The cronrepo program manage the cron jobs given a directory of cron job files. This is done by the followings:

  • Generation and installation

    # cronrepo generate <dir> --target <target>
    # cronrepo install <dir> --target <target>
    

    Generate cron jobs entries and show it on the command line or install them as cron jobs. Only jobs of the specific target is generated or installed. If not specified, generate/install all jobs. The job is started by a "cron runner" generated if you use "install".

  • Uninstallation

    # cronrepo uninstall <dir> --target <target>
    

    This undos the modification to your crontab, thus uninstalling the cron job. It also uninstalls the cron runner file generated during installation.

The cron runner

If you ever written a cron job you know that the environment as seen by the cron job is quite different from your normal environment: PATH is very simplistic (usually so simplistic that you end up setting up your PATH in your script as the first step). In cronrepo this is done for you.

In particular, when you use "cronrepo install", a "runner script" is created. All current environment variables (except a few) are converted into variable exporting commands in the runner script, and the current directory is also set in the script. So at the end, the cron jobs will run in a very similar environment as simply running the job on the command line of your terminal running "cronrepo install".

The trampoline

Normally, if a cron jobs fails or emit output, notification will be sent to the owner of the cron job via E-mail. You can globally disable this feature, but having nobody to take care of cron job failures is not a good idea.

You can write a program to actually run your cron jobs, and have the runner file to run that instead of the cron job. This is done by adding --trampoline "your_program" when you run cronrepo install. The arguments to your program is simply the path to the cron job file, as well as all the arguments to be passed to your job.

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

cronrepo-0.0.2.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cronrepo-0.0.2-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file cronrepo-0.0.2.tar.gz.

File metadata

  • Download URL: cronrepo-0.0.2.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.3.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.5.3

File hashes

Hashes for cronrepo-0.0.2.tar.gz
Algorithm Hash digest
SHA256 214e66b56425bc19c84075d60c2a4e98ed1b8e68978b725052a4503607ea463a
MD5 5d19d1666eab8c30196ea9a3eb304d34
BLAKE2b-256 06f5affdc2582b4483b4c2af26848c226f7a13aabdedc01bd0d08ad1261de9f2

See more details on using hashes here.

File details

Details for the file cronrepo-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: cronrepo-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.3.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.5.3

File hashes

Hashes for cronrepo-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6b171a42b9f82d12726f6508d12a71e471d50a44d01474d0c3822ecc8eac0a58
MD5 3c864b12465bc8d7e1cb65a1b049c196
BLAKE2b-256 609a1e1c2a24f8808e24c81962e01bbdfdfb1c7cbb1894976c31e9176291f913

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