Use Cron to Automate Tasks on Ubuntu LTS / CentOS

Use Cron to Automate Tasks
on Ubuntu / CentOS / REDHAR RHEL

Introduction

Linux’s distributions (Ubuntu, CentOS, RHEL) and other Unix-like operating systems have the time-based job scheduling daemon known as Cron. Cron is excellent for automating chores connected to maintenance since it works in the background and actions planned with it, known as “cron jobs,” are carried out automatically.

This tutorial offers instructions for scheduling jobs using the unique syntax of cron. It also discusses a few shortcuts you may take to speed up and improve the readability of task schedules.

Prerequisites

To complete this guide, you will need access to a server running Ubuntu LTS or CentOS.

No matter what sort of computer you use to follow this instruction, it should be set with a non-root user who has root access. To set up One, follow our guide on Creating a New sudo-enabled User in Ubuntu LTS & CentOS 7.

Step 1: Installing Cron

Nearly all Linux distribution features its own sort of cron installed by simply default. However, in the even that you are using the Ubuntu machine in which cron is not installed, you could mount it using APT.

Before installing cron on the machine, update the computer ‘s local package index:

Ubuntu

$ sudo apt update

CentOS

$ sudo yum update

And then install cron with the following command:

Ubuntu

$ sudo apt install cron

CentOS

$ sudo yum install cron

You will need to be sure it’s set to run in the background, too:

$ sudo systemctl enable cron
root@Ubuntu:~# sudo systemctl enable cron
Synchronizing state of cron.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable cron

Step 2 : Just how Cron Works

Cron jobs are documented and managed in a special document termed as a crontab. Every account on the system might have their own crontab where they can schedule jobs, which is stored under /var/spool/cron/crontabs/ .

To schedule a job, open up your crontab for editing and add a job written in the form of a cron expression. The particular syntax for cron expressions can be separated into two elements: the schedule and the command to execute.

You should use almost any command you would normally run using the order line. the plan component is split up into 5 different fields, which are written in the following command:

1st2nd3rd4th5th
*****
IDMinuteHourDay-DateMonthDay Name
Allowed Values0-590-231-311-12 or JAN-DEC0-6 or SUN-SAT

Each, task scheduled in a crontab is structured like the following :

$ * * * * *  <command> 
# OR 
$ * * * * * <path/to/script>
Example :

This command is set to run the job at 00:00 [midnight] every Sunday

0 0 * * 0 curl webhi.com
Note :
* : In cron expressions, an asterisk is a wildcard variable that represents “all.” Thus, a task scheduled with * * * * * will run every minute of every hour of every day of every month.
, : Commas break up scheduling values to form a list. If you want to have a task run at the beginning and middle of every hour, rather than writing out two separate tasks
- : A hyphen represents a range of values in the schedule field.
/ : You can use a forward slash with an asterisk to express a step value.
Note: It is important to note that you cannot express step values indiscriminately; you must use integers that divide evenly within the range permitted by the field in question. In the "hours" column, for example, you could only enter 1, 2, 3, 4, 5, 6, 7, or 12 after a forward slash.

Here are some further examples of how to utilize the scheduling component of cron:

* * * * * - Every minute.
12 * * * * - 12 minutes of after every hour.
(0,15,30,45 * * * *) / (*/15 * * * *) - Every 15 minutes.
0 4 * * * -Every day at 4:00 AM.
0 4 * * 2-4 -Every Tuesday, Wednesday, and Thursday at 4AM.
20,40 */8 * 7-12 * -Every day throughout the last six months of the year, on the 20th and 40th minutes of the 8 hour.

Step 3: Editing Crontabs

A cron, as previously stated, is a special file that stores the schedule of jobs that cron will run. However, these are not designed to be altered directly. It is instead advised that you use the crontab command. This allows you to change your user profile crontabs without having to use sudo. The crontab command will also notify you if there are any syntax mistakes in the crontab, but directly changing it would not.

You can modify your crontab by running the following command:

$ crontab -e
no crontab for bob - using an empty one
Select an editor.  To change later, run 'select-editor'.
  1. /bin/nano        <---- easiest
  2. /usr/bin/vim.basic
  3. /usr/bin/vim.tiny
  4. /bin/ed
Choose 1-4 [1]:

Enter the number corresponding to the editor of your choice. Alternatively, you might press ENTER to accept the default choice, nano.

When you use crontab -e in the future, it will automatically open your crontab in this text editor.

After you’ve made your choice, you’ll be led to a new crontab with some commented-out instructions on how to use it:

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command

You may use the following command to inspect the contents of your crontab and not edit it:

$ crontab -l

You may clear your crontab by running the following command:

Warning: The following command will not prompt you for confirmation that you wish to delete your crontab. Run it only if you are certain you want to delete it.
$ crontab -r

Conclusion

Cron is a flexible and powerful utility that can ease the burden of many tasks associated with system administration. Combined with shell scripts, it can automate normally tedious and complex tasks.

LEAVE A COMMENT