Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • 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 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

May 10, 2023 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:

FieldValue Range
Minutes0-59
Hours0-23
Day of a month1-31
Month1-12 or jan feb mar apr may jun jul aug sep oct nov dec
Week day0-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.

VariableDescriptionSyntax
@rebootRuns once at boot
@yearly

or

@annually

Once a year0 0 1 1 *
@monthlyOnce a month0 0 1 * *
@weeklyOnce a week0 0 * * 0
@dailyEvery day0 0 * * *
@hourlyEvery hour0 * * * *
@midnightAt 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="yourema[email protected]"
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

How to Increase Size of Disk Partition in...

October 5, 2023

How to Use Ansible to Manage Windows Machines

September 25, 2023

Fixing ‘The Network Path Was Not Found’ 0x80070035...

August 30, 2023

How to Install and Configure Ansible on Linux

August 27, 2023

Computer Doesn’t Turn Off After Shutting Down Windows...

August 26, 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

  • How to Connect VPN Before Windows Logon

    November 14, 2023
  • Removing Azure Arc Setup Feature on Windows Server 2022

    November 9, 2023
  • Using WPAD (Web Proxy Auto-Discovery Protocol) on Windows

    November 7, 2023
  • Send Emails with Microsoft Graph API and PowerShell

    November 6, 2023
  • Zabbix: How to Get Data from PowerShell Scripts

    October 27, 2023
  • Tracking Printer Usage with Windows Event Viewer Logs

    October 19, 2023
  • PowerShell: Configure Certificate-Based Authentication for Exchange Online (Azure)

    October 15, 2023
  • Reset Root Password in VMware ESXi

    October 12, 2023
  • How to Query and Change Teams User Presence Status with PowerShell

    October 8, 2023
  • How to Increase Size of Disk Partition in Ubuntu

    October 5, 2023

Follow us

  • Facebook
  • Twitter
  • Telegram
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?
  • Adding VLAN Interface in CentOS/Fedora/RHEL
  • Install and Configure SNMP on RHEL/CentOS/Fedor
  • Configuring High Performance NGINX and PHP-FPM Web Server
  • How to Install and Configure Squid Proxy Server on Linux
Footer Logo

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


Back To Top