Cron is a powerful scheduling tool used in Unix-like operating systems for running tasks at predetermined times. It’s invaluable for automating system maintenance and administrative tasks. However, when trying to schedule these tasks, you might encounter the error `-bash: crontab: command not found`. This indicates that the Crontab management utility is not installed on your system. This guide provides a detailed, step-by-step tutorial on how to install and configure Crontab, the interface for managing cron jobs, on CentOS and Red Hat Enterprise Linux (RHEL) systems.
Before diving into the installation process, it’s essential to understand what Crontab is and why it’s useful. Crontab, short for “cron table,” is a configuration file that specifies shell commands to run periodically on a given schedule. It is an incredibly efficient way to automate system maintenance and administration tasks.
- A system running CentOS or RHEL
- Access to a terminal or command line
- Sudo or root privileges
Step 1: Checking for Cron
The cron daemon is pre-installed on most Linux distributions, including CentOS and RHEL. Before proceeding with the installation, it’s wise to check if cron is already installed on your system. You can do this by running:
rpm -q cronie
If cron is installed, you’ll see the version number. If not, you’ll receive a message indicating that cron is not installed.
Step 2: Installing Cron
If cron is not already installed, you can easily install it using the package manager. For CentOS and RHEL systems, use the following command:
sudo yum install cronie
After installation, you’ll need to start and enable the cron service to ensure it runs on boot:
sudo systemctl start crond.service
sudo systemctl enable crond.service
Step 3: Configuring Crontab
With cron installed, you can now add scheduled tasks. To edit or create your crontab file, use the command:
This command opens the crontab file in your default editor. If you’re using crontab for the first time, this file will be empty.
Crontab syntax follows the pattern below:
* * * * * command to execute
- The first asterisk represents the minute (0 – 59)
- The second asterisk represents the hour (0 – 23)
- The third asterisk represents the day of the month (1 – 31)
- The fourth asterisk represents the month (1 – 12)
- The fifth asterisk represents the day of the week (0 – 7, where both 0 and 7 represent Sunday)
After the asterisks, you specify the command to be executed.
You can read more about editing crontab and its format using the following articles:
To run a script located at /home/user/backup.sh every day at 3 am, your crontab entry would look like this:
0 3 * * * /home/user/backup.sh
Step 4: Managing Crontab
To list all crontab jobs for the current user, use:
To remove your crontab file, effectively deleting all scheduled jobs, use:
Step 5: Securing Crontab
It’s essential to ensure that only authorized users can create or edit crontab files. Cron’s access control is managed through the files /etc/cron.allow and /etc/cron.deny. If cron.allow exists, only users listed in it can use crontab. If only cron.deny exists, all users listed are denied access to crontab.
Automating tasks on your CentOS or RHEL system using cron can save a significant amount of time and ensure that important tasks are not forgotten. This guide has walked you through installing cron, adding tasks, and managing crontab. With these basics, you can start automating your system tasks efficiently.