Skip to content

sosedoff/cron2

Repository files navigation

cron2

Cron2 is a job scheduling service similar to Cron

This is an experimental project - Please do not use in production

Features

  • Written in Go
  • Configuration is defined with HCL
  • Timezone support
  • Custom logging per job
  • Environment variables
  • Docker support
  • Test runs
  • Notifications

Motivation

Cron2 is created because i don't like standard Cron service that ships with most Linux boxes. Also, to mess around with HCL.

Install

go get -u github.com/sosedoff/cron2

Configuration

Basic example:

// Simple job that runs every minute.
job "simple" {
  spec = "* * * * *"
  command = "ping -c 1 google.com"
}

Customize timezone:

// Run command every day at 9am CST.
// The default time zone is UTC.
job "demp" {
  spec = "0 9 * * *"
  command = "rake db:backup"
  tz = "America/Chicago"
}

More configuration options:

job "demo" {
  spec = "0 9 * * *"
  command = "rake reports:generate"

  // Specify user for the job
  user = "deploy"

  // Change directory
  dir = "/home/deploy/app/current"

  // Custom log location
  log = "/var/log/myjob.log"

  // Configure environment variables
  env {
    RAILS_ENV = "production"
    DEBUG = "true"
  }

  // Configure max execution time
  timeout = "30min"

  // Setup notifications
  notify {
    // Configure delivery mode
    // Change to "all" to receive notifications for all runs
    on = "error"

    webhook {
      // Will send POST to this URL
      url = "https://mywebhook.com"
    }

    slack {
      // Set to incoming webhook URL
      url = "https://hooks.slack.com/services/..."

      // Set channel (optional)
      channel = "#ops"
      
      // Change username (optional)
      username = "cronbot"
    }
  }
}

Run with shell:

job "test" {
  // Commands are not run in shell by default
  shell = "bash"

  spec = "* * * * *"
  command = "cat /my/file | grep foobar | xargs gzip --best"
}

Inline scripts:

job "inline" {
  spec = "* * * * *"

  // You can write multi line bash scripts like this:
  command = <<END
    # Dump your bash script in here
    ls -al
    echo "line1"
    echo "line2"
  END
}

Docker configuration:

// Test command that will print out directory structure
job "docker test" {
  spec = "* * * * *"
  command = "ls -al"
  docker {
    image = "alpine:3.6"
  }
}

Testing jobs

Let's look at the example config: the job is going to be executed at 9am every day. We want to test the job without having to change the spec.

job "test" {
  spec = "0 9 * * *"
  tz = "America/Chicago"
  command = "do something"
}

Start the service first:

cron2 -config=/config.hcl -socket=/var/run/cron2.sock

Once the service is running, trigger the job:

cron2 -socket=/var/run/cron2.sock -trigger=test

If the job exists you'll see success message:

ok: scheduled

List jobs

You can see active jobs with:

cron2 -list

Output:

job1: active
job2: active
job3: inactive

About

Cron2 is cron scheduler on steroids: HCL, Docker, Logs, Notifications, Etc

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages