Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2012
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu
  • Home
  • About

Windows OS Hub

  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2012
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu

 Windows OS Hub / Linux / Configuring Cron Jobs with Crontab on CentOS/RHEL Linux

July 12, 2021 CentOSLinuxQuestions and AnswersRHEL

Configuring Cron Jobs with Crontab on CentOS/RHEL Linux

Cron is a task scheduler for Unix-based systems including all Linux distros. The cron daemon works in the background on your host and runs scheduled tasks according to the schedule. In this article, we will show how to install cron on a server running CentOS or RHEL Linux, learn the cron syntax, and schedule cron jobs with crontab.

Contents:
  • How to Install Cron on Centos or RHEL Linux?
  • How to Add a Cron Jobs with Crontab?
  • How to Send Cron Notifications to Email?
  • Cron Configuration Files & Logs

How to Install Cron on Centos or RHEL Linux?

By default, cron is available immediately after RHEL or CentOS installation. If you don’t have it for some reason, you can install it from the base repository using the yum or dnf command:

# dnf update -y — to update all packages on a host
# dnf install crontabs -y — to install cron

install crontab on Linux RHEL/CentOS/Fedora

Enable the crond daemon and run it after the installation:

# systemctl enable crond.service
# systemctl start crond.service

How to Add a Cron Jobs with Crontab?

You can use the following command to add tasks to cron:

# crontab -e

This command will open a task file for your user in a default text editor (it is vim in my case, but you can change it to the one that is more convenient to you, for example, nano). This method to configure tasks prevents syntax errors. Crontab doesn’t allow saving a config file containing errors.

You can also edit the cron jobs file manually in mc:

# mcedit /var/spool/cron/root – a file name may be different depending on the user.

To add a simple job that runs a bash script in cron, enter this command:

# crontab -e

Then add the task schedule and the path to the script file:

* * * * * /root/test.sh

Save the file (it is similar to editing in vim: press Ctrl+O to save a file and Ctrl+X to exit).

If you have done it correctly, your task will be added to cron. To display the list of cron jobs, run the following command:

# cat /var/spool/cron/root

* * * * * /root/test.sh

Or this one:

# crontab -l

This script will run through cron every minute.

The minimum time is 1 minute. The cron daemon scans the list of tasks once a minute. It checks the following files and directories:

/etc/crontab
/etc/cron.*/.
/var/spool/cron/

Each crontab schedule entry consists of 5 fields:

minutes hours day_of_a_month months week_day
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed

The cron schedule expression syntax

You can use the following valid values for each of the fields:

Field Value Range
Minutes 0-59
Hours 0-23
Day of a month 1-31
Month 1-12 or jan feb mar apr may jun jul aug sep oct nov dec
Week day 0-6 (where 0 is Sunday) or sun mon tue wed thu fri sat

The * character means all allowed values. Here is a sample task:

30 00 * * 1 /root/test.sh

The script in the task will be run every Monday at 00:30 AM.

To make the cron file syntax easier, some special characters are used:

  • A comma (,) is used to separate schedule values to run the same task at different times. For example, if you want to run a task at the 15th and 30th minute of every hour, you can set the schedule as follows:
15 * * * *
30 * * * *

Or use a shorter syntax with the comma:

15,30 * * * *
  • A slash (/) is used to repeat a task. For example, you want to run a task every 2 hours. Using / you will make the contents of a cron file much shorter, otherwise, it is quite lengthy:
* */2 * * *
  • A hyphen (-) indicates the range of values in a field. If you want to run a task for the first or last 10 minutes of an hour, specify the range using a hyphen:
0-10 * * * *
50-60 * * * *

Here are some more examples of cron schedules:

  • to run on weekdays at 12:00 PM and at 06:00 PM: 0 12,18 * * 1-5
  • every 30 minutes: */30 * * * *
  • each Saturday: 0 0 * * 6
  • every Tuesday and Thursday at 02:00 AM: 0 2 * * 2,4

You can also use special variables in cron.

Variable Description Syntax
@reboot Runs once at boot
@yearly

or

@annually

Once a year 0 0 1 1 *
@monthly Once a month 0 0 1 * *
@weekly Once a week 0 0 * * 0
@daily Every day 0 0 * * *
@hourly Every hour 0 * * * *
@midnight At midnight

It means that to run a task every day, you can use the following cron syntax:

@daily echo "Cron check"

You can edit a crontab file of another user:

# crontab -u username

How to Send Cron Notifications to Email?

If you want to receive information about running your crontab tasks by email, you need to configure the cron file with the job.

To send emails, a mail agent must be installed on your server. To do a test, I have installed sendmail on my Linux host:

# dnf install sendmail -y
# service sendmail start

Let’s configure the parameters to send emails in the cron file. Add the following lines to the file:

MAILTO="youremail@gmail.com"
SHELL=/bin/bash
HOME=/
* * * * * echo "Cron check"

SHELL — a user shell

HOME — a path to the cron file

Enable Cron Notifications to Email on Linux

Every time a cron job starts, an email notification will be sent to your mailbox.

You can save the information about running a cron task to a log file. To do it, add >> to the end of the file and enter a path to your log file:

* * * * * echo "Cron check" >> /var/log/admin/journal.log

If there are many jobs in your crontab file, and you don’t want to get the results of some of them by email, you can run these jobs in a silent mode:

* * * * * echo "Cron check" >> /dev/null 2>&1

Cron Configuration Files & Logs

The main cron configuration file is /etc/crontab. Besides the cron file, you can run jobs from the following directories:

  • /etc/cron.daily – to start scripts once a day
  • /etc/cron.hourly – …. once an hour
  • /etc/cron.monthly – …. once a month
  • /etc/cron.weekly – …. once a week

Just put a script file in one of the directories to run it according to the schedule.

You can restrict access to the scheduler using /etc/cron.allow and /etc/cron.deny. It is enough to create these files and add users to them, who are allowed or denied to run cron tasks.

You can add jobs to the /etc/crontab as well. Usually, the file is used by the root user or to configure system tasks. Personal user files of cron jobs are stored in the /var/spool/cron/ or /var/cron/tabs/.

To track cron jobs or errors, you can view the log file: /var/log/cron. This file records all tasks and errors in the daemon operation if any:

crontab logfile

0 comment
0
Facebook Twitter Google + Pinterest
previous post
How to Install Office 365 ProPlus on RDS (Terminal) Server?
next post
How to Install and Update Group Policy Administrative Templates (ADMX)?

Related Reading

Enable Internet Explorer (IE) Compatibility Mode in Microsoft...

January 27, 2023

How to Disable or Uninstall Internet Explorer (IE)...

January 26, 2023

How to Stop Automatic Upgrade to Windows 11?

January 18, 2023

Fix: Windows Needs Your Current Credentials Pop-up Message

January 18, 2023

Adding Trusted Root Certificates on Linux

January 9, 2023

Leave a Comment Cancel Reply

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMWare
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • Using Previous Command History in PowerShell Console

    January 31, 2023
  • How to Install the PowerShell Active Directory Module and Manage AD?

    January 31, 2023
  • Finding Duplicate E-mail (SMTP) Addresses in Exchange

    January 27, 2023
  • How to Delete Old User Profiles in Windows?

    January 25, 2023
  • How to Install Free VMware Hypervisor (ESXi)?

    January 24, 2023
  • How to Enable TLS 1.2 on Windows?

    January 18, 2023
  • Allow or Prevent Non-Admin Users from Reboot/Shutdown Windows

    January 17, 2023
  • Fix: Can’t Extend Volume in Windows

    January 12, 2023
  • Wi-Fi (Internet) Disconnects After Sleep or Hibernation on Windows 10/11

    January 11, 2023
  • Adding Trusted Root Certificates on Linux

    January 9, 2023

Follow us

woshub.com
  • Facebook
  • Twitter
  • RSS
Popular Posts
  • How to Configure MariaDB Master-Master/Slave Replication?
  • How to Mount Google Drive or OneDrive in Linux?
  • KVM: How to Expand or Shrink a Virtual Machine Disk Size?
  • Hyper-V Boot Error: The Image’s Hash and Certificate Are not Allowed
  • Adding VLAN Interface in CentOS/Fedora/RHEL
  • Configuring High Performance NGINX and PHP-FPM Web Server
  • Install and Configure SNMP on RHEL/CentOS/Fedor
Footer Logo

@2014 - 2023 - Windows OS Hub. All about operating systems for sysadmins


Back To Top