Operations 11 min read

Master Linux Cron: From Basics to Advanced Scheduling

This guide explains the Linux cron daemon, how to control the crond service, configure system and user crontabs, manage permissions, create custom cron scripts, use the crontab command syntax, and provides numerous practical scheduling examples.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Linux Cron: From Basics to Advanced Scheduling

What is cron and crond?

cron is the Linux daemon that runs scheduled tasks automatically, similar to Windows Task Scheduler. After the OS installation, the crond service starts by default and checks every minute for jobs to execute.

Managing the cron service

service crond start   # start the service
service crond stop    # stop the service
service crond restart # restart the service
service crond reload  # reload configuration
service crond status  # show service status

Cron configuration files

/var/spool/cron/ stores each user’s personal crontab file (e.g., /var/spool/cron/tom). Typically a user has only one file.

/etc/crontab is used by the system administrator to define system‑wide scheduled tasks. A typical header looks like:

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

The file also contains a commented example that illustrates the five‑field time specification followed by the user name and command.

/etc/cron.d/ can hold additional crontab files or scripts.

/etc/cron.hourly , /etc/cron.daily , /etc/cron.weekly , and /etc/cron.monthly are directories whose executable files are run automatically at the corresponding intervals (e.g., 01 * * * * root run-parts /etc/cron.hourly runs every hour at minute 01).

Permission control

Permission is governed by /var/adm/cron/cron.allow and /var/adm/cron/cron.deny (or their equivalents on other Unix variants):

If neither file exists, only root may use crontab.

If cron.allow exists and cron.deny does not, only users listed in cron.allow (including root if listed) may use crontab.

If cron.allow is absent but cron.deny exists, all users except those listed in cron.deny may use crontab.

If both exist, a user must be listed in cron.allow and not in cron.deny to be allowed; cron.allow takes precedence when a user appears in both.

Creating a cron script

Step 1: Write a script file, e.g., crontest.cron:

15,30,45,59 * * * * echo "xgmtest....." >> xgmtest.txt

Step 2: Install it with crontab crontest.cron. This replaces the current crontab for the user.

Step 3: Verify with crontab -l or by checking the generated file under /var/spool/cron.

Using the crontab command

The crontab utility installs, removes, or lists a user's cron table. Each line in a crontab consists of six fields: minute, hour, day‑of‑month, month, day‑of‑week, and the command to run.

minute hour day-of-month month day-of-week command
00-59  00-23 01-31          01-12   0-6 (0=Sunday)

Special characters: * – every possible value / – step values (e.g., */5 means every 5 units) - – range (e.g., 1-5) , – list of discrete values

Common options: -l – list current crontab -r – remove current crontab -e – edit using the editor defined by VISUAL or

EDITOR

Practical scheduling examples

Every morning 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 11 PM and 8 AM, every two hours plus 8 AM:

0 23-7/2,8 * * * echo "Have a good dream" >> /tmp/test.txt

On the 4th of each month at 11 AM, Monday‑Wednesday: 0 11 4 * 1-3 command Run all scripts in /etc/cron.hourly at minute 01 of every hour: 01 * * * * root run-parts /etc/cron.hourly Run a user‑specific command at 10 and 40 minutes past each hour: 10,40 * * * * /home/user/innd/bbslink These examples illustrate how to combine time fields, user specifications, and command execution to meet a wide range of automation needs.

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.

OperationsSchedulingcronSystem Administrationcrontab
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

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.