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 many related cron jobs and to ensure that they are installed exactly at the moment when the repository gets deployed. This is 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, and need to allow a comment style headed by "#" (or otherwise allowing such lines to be inserted freely, e.g., through a multi-line string syntax). 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 is invoked 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:5: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 two more features of the tagline:

  • We can put an integer between the double-colon to give a level number to the job. The default level is 0. This is useful in the show-inv command described below.
  • 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 manages 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.

  • Listing invocations

    # cronrepo list-inv <dir> --target <target> --minlevel <level> \
          --start <dt> --end <dt>
    

    This lists the expected invocations of the cron job entries that occur between the specified <dt> (datetime, in 'YYYY-mm-ddTHH:MM'), inclusive. Jobs are listed only if it has a level of at least <level>. The output is in a format that you can save and run in the shell.

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, followed by all the arguments to be passed to your job (as specified in + ... in the cron job file).

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.2.1.tar.gz (9.0 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.2.1-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: cronrepo-0.2.1.tar.gz
  • Upload date:
  • Size: 9.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.5

File hashes

Hashes for cronrepo-0.2.1.tar.gz
Algorithm Hash digest
SHA256 52080347a334404da0dcecdb92d82864578046ce8ec3ffe878faa89ada091fef
MD5 b60f64ad0b31d613f553ba92c49d5a9f
BLAKE2b-256 2c1c50919ebe6e920f30a966bcdabdaf2c925335f1a488b0367f8b3a835d2163

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cronrepo-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.5

File hashes

Hashes for cronrepo-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c80de873cd0b7c8747f6084e706a8ee0ab404c3255926d071c05b41f9fc5b515
MD5 95833fdf1f832171a0dd661861601196
BLAKE2b-256 886f957c631c55d79963b1fc4a39fac2585548f7bd9889773383ad98a537e3b2

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