Bobcares

Digitalocean Cron Job

by | Jun 1, 2022

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

Bobcares, as part of our Digitalocean managed service, answers to all queries.

Let’s find more about cron job in the Digitalocean platform.

Digitalocean Cron Job

Some 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.

Installing cron

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.

  1. Firstly, make sure the system’s local package index is up to date.

    sudo apt update

  2. Then install cron with the below command:

    sudo apt install cron

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

    sudo systemctl enable cron

Now cron is installed on the system and ready to start scheduling jobs.

Understanding How Cron Works

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 http://www.google.com

30 17 * * 2 curl http://www.google.com

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

  1. *: 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.
  2. ,: To make a list, use commas to separate the scheduling values.
  3. -: In the schedule field, a hyphen represents a range of values.
  4. /: A step value is represented by a forward slash with an asterisk.

Managing Crontabs

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 profile’s 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.

  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

[Looking for a solution to another query? We are just a click away.]

Conclusion

To sum up, our Support team demonstrated how to install cron in the Digitalocean platform.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.