CentOS 7 Cron Task Setup Guide

On CentOS systems, cron is the go-to utility for scheduling recurring tasks, though three scheduling tools exist for different use cases:

at         One-time execution only; requires the atd backend daemon to run
cronie/crontab   Recurring task execution; requires the crond daemon, with tasks managed via crontab
anacron    Daily-based scheduling only; automatically catches up missed tasks after a system restart

Cronie, Crond, and Crontab Basics

To use crontab effectively, you must understand its relationship with cronie and crond. The cronie package provides both crond—the core daemon that checks for pending tasks every minute by default, making it unsuitable for sub-minute scheduling—and crontab—the CLI tool for creating, viewing, and modifying task lists.

Installation

Cronie is preinstalled on most CentOS 7 instances. If missing, install it via yum:

yum install -y cronie

Core Management Commands

Crond Daemon Controls

systemctl start crond        # Start the scheduling daemon
systemctl stop crond         # Stop the daemon
systemctl restart crond      # Restart the daemon
systemctl reload crond       # Reload task configurations without restarting
systemctl status crond       # Check current daemon status

Crontab Task Management

crontab -u <username>        # Operate on a specific user's task list
crontab -l                   # List the current user's scheduled tasks
crontab -e                   # Edit the current user's task list
crontab -r                   # Delete the current user's entire task list
crontab -i                   # Delete the task list with a confirmation prompt

Task Configuration Methods

Standard Task Syntax

All user-specific cron tasks use a 6-field structure, while system-wide tasks (in /etc/crontab) add a username field as the 7th. The syntax is explained with comments in /etc/crontab:

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# Field breakdown:
# .---------------- minute (0-59)
# |  .------------- hour (0-23, 0 = midnight)
# |  |  .---------- day of month (1-31)
# |  |  |  .------- month (1-12 or jan/feb/mar...)
# |  |  |  |  .---- day of week (0-6 or sun/mon/tue..., 0/Sun = Sunday)
# |  |  |  |  |
# *  *  *  *  *  <username>  <command>  # System-wide format only
# *  *  *  *  *  <command>              # User-specific format

Special characters modify field behavior:

  • *: Matches any valid value for the field (e.g., * in minute = every minute)
  • -: Defines a continuous range (e.g., 10-12 in hour = 10 AM, 11 AM, 12 PM)
  • ,: Lists discrete values (e.g., 15,45 in minute = 15 and 45 minutes past the hour)
  • /n: Runs tasks every n intervals (e.g., */5 in minute = every 5 minutes)

1. System-Wide Configuration via /etc/crontab

Edit /etc/crontab directly and append tasks using the 7-field system-wide format. This is ideal for tasks that require elevated privileges or should apply regardless of user login status.

2. User-Specific Configuration via crontab -e

Running crontab -e opens the current user's task file in the default editor. Tasks added here use the 6-field format, and crond runs them under the current user's permissions.

Example:

*/3 * * * * /home/devops/backup_logs.sh

3. Directly Modify User Task Files in /var/spool/cron/

Cron stores user-specific task lists in /var/spool/cron/, with filenames matching usernames. Editing these files directly is equivalent to using crontab -u <username> -e.

Practical Examples

* * * * * /usr/local/bin/health_check.sh                # Run every 1 minute
20,50 8-10 * * mon-fri /usr/bin/sync_database.sh       # 8:20, 8:50, 9:20, 9:50, 10:20, 10:50 on weekdays
0 */4 * * * /home/admin/clean_temp.sh                    # Run every 4 hours at midnight, 4 AM, 8 AM, etc.
30 2 1,15 * * /usr/bin/rotate_archives.sh                # 2:30 AM on the 1st and 15th of every month

Key Cron Files

/etc/cron.deny     # Blocklisted users cannot use crontab
/etc/cron.allow    # Allowlisted users (takes priority over cron.deny)
/var/spool/cron/   # Directory containing all user-specific task lists
/var/log/cron      # Cron daemon activity log

Common Troubleshooting Tips

  1. Configuration Reloads: In most CentOS 7 setups, crond automatically detects changes to task files within 1 minute, so manual reload or restart is often unnecessary.
  2. Lock File Errors: If you encounter (CRON) DEATH (can’t lock /var/run/crond.pid, otherpid may be XXXX): Resource temporarily unavailable, first verify your task syntax is correct, then kill all existing crond processes with pkill crond and restart the daemon with systemctl start crond.

Tags: CentOS 7 Linux cron crontab Scheduled Tasks

Posted on Wed, 20 May 2026 00:10:13 +0000 by Vertical3