Skip to main content
Tools

Managing Cron Jobs Across Multiple Servers Using Dkron

A modern, advanced tool to manage cron jobs across a range of servers in your deployment.

LHB Community

Warp Terminal

Automation of repetitive tasks is a key component in IT management. It not only saves time, but also reduces the risk of errors.

This is where Dkron comes in. With its ability to orchestrate cron job workflows across multiple machines, it simplifies the lives of DevOps system administrators.

Why Dkron?

Unlike a traditional cron, Dkron is designed to work in a distributed environment. This means it can handle tasks scheduled across multiple servers, providing much greater flexibility and scale. Additionally, Dkron ensures high availability, which is crucial for critical operations that cannot tolerate interruptions.

Another major advantage of Dkron is its ease of use. It provides an intuitive user interface and API , allowing users to configure, monitor and manage their cron jobs with great simplicity. Whether for simple tasks or complex workflows, Dkron proves to be a powerful ally.

Installation and Basic Configuration of Dkron

Installation and initial configuration of Dkron are the first steps toward effective management of scheduled tasks in your infrastructure.

The process of installing Dkron varies slightly depending on your server's operating system.

Dkron is available for Linux, MacOs and FreeBSD. Dkron can be installed by directly downloading the binaries from the Dkron project page.

It is also available as a Docker image, making it easier to deploy in containerized environments.

docker run -d -p 8080:8080 --name dkron dkron/dkron agent --server --bootstrap-expect=1 --node-name=node1

Once Dkron is installed, the next step is basic configuration. This initial configuration is crucial to ensure the proper functioning of your cron jobs .

Dkron uses a configuration file in JSON or YAML format. This file contains essential settings such as execution mode (server or agent), cluster node addresses, and other customization options.

Creation and Management of Cron Jobs with Dkron

Once Dkron is installed and configured, the next step is the creation and management of cron jobs.

Creating a job in Dkron begins with defining its key characteristics:

  • Job Name : Each job must have a unique name, which makes it easier to identify and manage.
  • Schedule : Define how often the job should run. Dkron uses standard cron syntax, allowing great flexibility in scheduling.
  • Command to Execute : Specify the command or script that the job should execute. This command must be idempotent, that is, produce the same result if executed several times under the same conditions.

It is possible to create jobs via the dkron API. An example with the curl command:

curl localhost:8080/v1/jobs -XPOST -d '{
  "name": "job1",
  "schedule": "@every 10s",
  "timezone": "Europe/Berlin",
  "owner": "Platform Team",
  "owner_email": "[email protected]",
  "disabled": false,
  "tags": {
    "server": "true:1"
  },
  "metadata": {
    "user": "12345"
  },
  "concurrency": "allow",
  "executor": "shell",
  "executor_config": {
    "command": "date"
  }
}'

Advance Configuration

For specific needs, you can configure advanced settings:

  • Tags: Tags allow you to control on which node of the Dkron cluster the job should run.
  • Retries: Configure the number of retries in case the job fails.
  • Notifications: Configure notifications to be informed of job status. Dkron supports multiple notification channels such as emails, Slack, or webhooks.
  • Environmental Parameters: Inject environment variables into your jobs to modify their behavior depending on the execution context.
  • Custom Scripts: Use custom scripts for complex tasks, providing increased flexibility over simple commands.
  • Webhooks and API: Use webhooks and the Dkron API to integrate with other systems, such as monitoring systems, databases, or CI/CD platforms.
  • Plugins: Explore available plugins or develop your own to extend Dkron's capabilities according to your specific needs.

Dkron provides the ability to manage dependencies between tasks. This means that you can configure jobs to only run when certain conditions or prerequisites are met.

{
  "name": "job1",
  "schedule": "@every 10s",
  "executor": "shell",
  "executor_config": {
    "command": "echo \"Hello from parent\""
  }
}

{
  "name": "child_job",
  "parent_job": "job1",
  "executor": "shell",
  "executor_config": {
    "command": "echo \"Hello from child\""
  }
}
  • State-Based Triggers: Configure jobs to trigger in response to the successful completion or failure of other jobs.
  • Job Scheduling: Ensure precise job scheduling to maintain the integrity of your business processes and IT operations.

Dkron in Action: Scheduling Tasks Across Multiple Servers

Let's imagine that you have a distributed application deployed across multiple servers, and you want to perform a routine maintenance task, such as cleaning up temporary files, every day at midnight.

Given each server is configured to communicate with the Dkron cluster, all you have to do is to define the cron job and specify the addresses of all cluster nodes in the Dkron configuration file.

Define the Cron Job

Let's create a cron job named "cleanup" that runs maintenance script every day at midnight. You can use Dkron's API to define this job:

  curl localhost:8080/v1/jobs -XPOST -d '{
  "name": "cleanup",
  "schedule": "0 0 * * *",
  "executor": "shell",
  "executor_config": {
    "command": "/path/to/cleanup_script.sh"
  }
}'

In this example, the job is scheduled to run at 12:00 AM every day using standard cron syntax using the shell command at the specified path.

Specify Node Tags

For the cleanup job to run on all servers in our cluster, we can use tags to target all nodes. Modify the job creation command to include tags for each server:

curl localhost:8080/v1/jobs -XPOST -d '{
  "name": "cleanup",
  "schedule": "0 0 * * *",
  "executor": "shell",
  "executor_config": {
    "command": "/path/to/cleanup_script.sh"
  },
  "tags": {
    "server": "true:1"
  }
}'

Once the job is created, you can monitor its status and manage it using Dkron's API. You can see when the job was last executed, any errors encountered, and other relevant information.

As the infrastructure grows, Dkron can easily be scaled to accommodate additional servers. Its distributed architecture ensures high availability, so even if one node fails, the scheduled tasks continue to execute on the remaining nodes without interruption.

Wrapping Up

Dkron, with its ease of use, robustness and flexibility, allows efficient management of scheduled tasks. Its advanced features, such as managing dependencies between tasks and recovery after failure, make it an essential tool for automating certain DevOps processes. Especially since there is no equivalent open source solution allowing you to schedule job workflows with a such efficiency.

✍️
Author: Talha Khalid is a freelance web developer and technical writer.
LHB Community