The cron job in Digitalocean is a time-based task scheduler that runs in the background and executes automatically.
Here at Ibmi Media, we regularly fix related Digitalocean queries.
In this context, we shall look into cron job in the Digitalocean platform.
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.
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
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:
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:
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
This article covers how to install and use cron in the Digitalocean platform. In fact, you can now start scheduling jobs in the server.
Almost all Linux distributions comes with pre installed cron by default. In case if its not installed on your machine, you can install it using following command:
# apt-get install cron
Now check the status of cron service, it should be running in the background by default, you can start it with start command if its in stopped state:
# service cron start
# service cron status
Once your cron service is fine you can proceed forward and start using it. For more help about its usage you can check its man pages by executing below command:
# man crontab