> HackerTyper Blog

How to Use Cron Functionality in Dokku

Dokku is an open-source Platform as a Service (PaaS) that allows developers to deploy and manage their applications. One of the powerful features of Dokku is its ability to schedule tasks using Cron. This makes it easier to manage compared to other PaaS applications like Caprover, where to run Cron, you will need to setup a whole independent container to run Cron.

What is Dokku?

Dokku is a lightweight PaaS that provides a simple and easy-to-use deployment process for developers. It is built on top of Docker and uses Git to deploy applications. Dokku supports a wide range of programming languages, including Ruby, Node.js, PHP, Python, and more.

Dokku's goal is to make deploying and managing applications as easy as possible. It does this by providing a simple and intuitive command-line interface that abstracts away the complexities of Docker and containerization.

What is Cron?

Cron is a time-based job scheduler in Unix-like operating systems. It allows users to schedule tasks to run at specific intervals or times. Yu can use Cron jobs for a variety of tasks, such as backing up data, cleaning up temporary files, or sending emails. Cron syntax consists of five fields that specify when the task should be executed: minute, hour, day of the month, month, and day of the week. You can schedule Cron jobs to run at any interval, from every minute to every year.

An example if you wanted to run backup.sh script every morning at 2:30am you would use:

30 2 * * * /app/bin/backup.sh

To write Cron syntax, I would recommend using a website like crontnab guru

How Dokku and Cron work together

Dokku uses the Cron service of the host system to run commands inside the applications. This means that the actual host system (where Dokku runs) is the one that schedules the commands, and runs them by using the run command.

For example, if we had the following Cron job:

0 * * * * npm run send-mail

Then, dokku will execute the following:

dokku run my-app npm run send-mail

How to create Cron jobs

You can create Cron jobs in Dokku by including them in the app.json file of their application. The app.json file is a manifest file--which should be in the root of you application--that defines the configuration and settings of an application, including any Cron jobs.

To add a Cron job to your application, you'll need to include the following code in your app.json file:

	"cron": [
			"command": "npm run send-mail",
			"schedule": "0 * * * *"

And then deploy your application . Dokku will pick up the task from the app.json file and schedule them.

Logging output from your Cron jobs

This is the part that I feel like the Dokku docs don't do a good job at covering.

By default, the output of Cron jobs is sent to /dev/null, which means that it is discarded. But, you can redirect the output to a log file by adding the following line to your Cron job:

30 2 * * * your-command >> /app/logs/backup.log 2>&1

The thing to note here, it's that the log will be in the host system and that the dokku user needs to have write access to it.

You can give that file ownership to dokku like so:

# Create the file
touch /app/logs/backup.log
# Change ownership
sudo chmod dokku: /app/logs/backup.log

You can also see the output of the Cron service in the host system in /var/log/syslog. Remember, this is the output of the service itself and not of the commands you run. Iit can be a good starting point to debug any issues.

Wrapping up

In conclusion, using the Cron functionality in Dokku can be a powerful tool for developers looking to automate tasks in their applications. With a simple and intuitive command-line interface, setting up Cron jobs in Dokku is easy and straightforward. Just remember Cron always runs in the host system but the commands themselves run in your application.