Operations 11 min read

Master Linux Cron: Essential Commands, Config Files, and Scheduling Tricks

This guide explains Linux’s cron daemon, covering service control commands, key configuration files such as /var/spool/cron and /etc/crontab, permission handling, script creation, crontab syntax with special characters, and practical scheduling examples for effective system task automation.

Raymond Ops
Raymond Ops
Raymond Ops
Master Linux Cron: Essential Commands, Config Files, and Scheduling Tricks

What is cron?

cron is the Linux daemon for scheduling periodic tasks, similar to Windows Task Scheduler. It runs as crond, checking every minute for jobs to execute.

Cron Service Commands

service crond start   // start service
service crond stop    // stop service
service crond restart // restart service
service crond reload  // reload config
service crond status  // view status

Cron Configuration Files

/var/spool/cron/ holds each user’s crontab file, named after the user (e.g., /var/spool/cron/tom). Typically one file per user.

/etc/crontab is the system‑wide crontab managed by the administrator. Example content includes environment variables and the schedule syntax.

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# minute hour day‑of‑month month day‑of‑week user command
# * * * * * user-name command

MAILTO specifies where error output is mailed; users often change it to their own address.

/etc/cron.d Directory

Files placed in /etc/cron.d are treated as additional crontab files.

Cron Permissions

Permission is controlled by /var/adm/cron/cron.allow and cron.deny. The rules are:

If neither file exists, only root can use crontab.

If only cron.allow exists, only listed users may use crontab.

If only cron.deny exists, all users except those listed may use crontab.

If both exist, a user must be in cron.allow and not in cron.deny.

Creating a Cron Script

Write a script file (e.g., crontest.cron) and install it with crontab crontest.cron. Use crontab -l to verify.

crontab Syntax

Each line consists of six fields: minute, hour, day‑of‑month, month, day‑of‑week, and the command.

minute hour day‑of‑month month day‑of‑week command

Valid ranges: minute 0‑59, hour 0‑23, day‑of‑month 1‑31, month 1‑12, day‑of‑week 0‑6 (0 = Sunday).

Special Characters

Use * for all values, / for step values (e.g., */5 every 5 units), - for ranges, and , to separate discrete values.

Common crontab Options

-l

list current crontab. -r remove current crontab. -e edit using $VISUAL or $EDITOR.

Example Schedules

# Every day at 6 am
0 6 * * * echo "Good morning." >> /tmp/test.txt

# Every two hours
0 */2 * * * echo "Have a break now." >> /tmp/test.txt

# Between 23:00 and 07:00 every two hours, plus 08:00
0 23-7/2,8 * * * echo "Have a good dream" >> /tmp/test.txt

# 4th of each month at 11 am on Mon‑Wed
0 11 4 * 1-3 command

# Jan 1 at 4 am
0 4 1 1 * command
Cron schedule diagram
Cron schedule diagram
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxSchedulingcronSystem Administrationcrontab
Raymond Ops
Written by

Raymond Ops

Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.