The Cron Job in Digitalocean - How it works ?


The cron job in Digitalocean is a time-based task scheduler that runs in the background and executes automatically.

Here at Fixwebnode , we regularly fix related Digitalocean queries.

In this context, we shall look into cron job in the Digitalocean platform.


More about Cron Job in Digitalocean Server ?                            

Most applications require the server to perform administrative tasks on a regular basis, such as sending emails to users, backing up important data, or cleaning up database tables. At the end of the day, a task scheduling mechanism is required to handle these tasks. Cron is a task scheduler for Linux and Unix systems that can be used to automate maintenance tasks.


How to Install cron in Digitalocean ?                            

Most Linux distributions come with some form of cron pre-installed. However, while using Ubuntu machine on which cron isn't installed, it is needed to install cron using APT.

So follow the below steps:

1.  To begin, make sure the system's local package index is up to date by running the below command via terminal:

$ sudo apt update

2. Next, install cron with the below command:

$ sudo apt install cron

3. Now, ensure that it is also set to run in the background:

$ sudo systemctl enable cron

How does Cron Work in Digitalocean ?                            

A crontab is a special file that records and manages cron jobs. Each user profile on the system can have their own crontab, which is stored in /var/spool/cron/crontabs/ , where they can schedule jobs.

Open the crontab  for editing and write the task in the form of a cron expression to schedule a job. The schedule and the command to run are the two parts of the cron expression syntax.

Almost every command that runs on the command line can be used as the command. The schedule component of the syntax is broken down into 5 different fields and can specify in the following order:

  • Minute : 0-59 are the allowed values.
  • Hour : 0-23 are the allowed values.
  • Day of the month : 1-31 are the allowed values.
  • Month: 1-12 or JAN-DEC are the allowed values.
  • Day of the week: 0-6 or SAT-SUN are the allowed values.


Tasks scheduled in a crontab are organised in the following way:

minute hour day_of_month month day_of_week command_to_run

An example of a cron expression. Each Tuesday at 5:30 p.m.,the expression executes the command curl

30 17 * * 2 curl

To make scheduling tasks easier, include the following special characters in the schedule component of a cron expression:

  • *:  An asterisk in cron expressions is a wildcard variable that represents "all". Thus, a task scheduled with * * * * *… will run every minute, hour, day, and month.
  • ,:  To make a list, use commas to separate the scheduling values.
  • -:  In the schedule field, a hyphen represents a range of values.
  • /:  A step value is represented by a forward slash with an asterisk.


How to manage Crontabs in Digitalocean ?                            

A crontab is a special file that contains the schedule of jobs cron will run. These are not meant to be edited directly. So it's preferable to use the crontab command which allows to edit a user profiles crontab without changing the privileges with sudo. 

The crontab command will also inform about the syntax errors in the crontab, whereas editing it directly will not.

So follow the below steps:

1. We can edit the crontab using the command:

$ crontab -e

While running crontab -e in the future, it will automatically open the crontab in this text editor. Once in the editor, type the schedule on a new line for each job.

2. Then, to view the contents of the crontab without editing, use the following command:

$ crontab -l

3.  To remove the crontab, use the below command:

$ crontab -r

[Need help in fixing Cron issues ?   We can help you ]                            


This article covers how to install and use cron in the Digitalocean platform. In fact, you can now start scheduling jobs in the server.

Your Cart