Operations 11 min read

Master Linux Cron: Schedule Tasks Efficiently with Real-World Examples

This guide explains how Linux's cron daemon works, how to manage the crond service, configure system and user crontabs, set environment variables, write cron scripts, and use common scheduling syntax with practical examples.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Linux Cron: Schedule Tasks Efficiently with Real-World Examples

cron is the Linux daemon for scheduling periodic tasks, similar to Windows Task Scheduler. After OS installation, the crond service starts automatically and checks every minute for jobs to run.

cron service commands

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

Configuration files

Each user’s crontab is stored in /var/spool/cron/ named after the user (e.g., /var/spool/cron/tom). Typically a user has only one crontab file.

The system‑wide file /etc/crontab is maintained by the administrator and can schedule tasks for any user. Example header:

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12)
# | | | | .---- day of week (0 - 6, Sunday=0 or 7)
# * * * * * user-name command to be executed

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

Directory /etc/cron.d/

Stores additional crontab files or scripts.

Creating a cron script

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

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

2. Install it with crontab crontest.cron.

3. Verify with crontab -l or check /var/spool/cron.

crontab command usage crontab installs, removes, or lists tables that drive the cron daemon. Each user can have a personal crontab; files under /var/spool/cron are not edited directly.

A crontab line consists of six fields: minute, hour, day‑of‑month, month, day‑of‑week, and the command. Valid ranges are 0‑59 for minutes, 0‑23 for hours, 1‑31 for day‑of‑month, 1‑12 for month, and 0‑6 for day‑of‑week (0 = Sunday).

minute hour day-of-month month day-of-week command

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

Common options:

-l   display current crontab
-r   remove current crontab
-e   edit current crontab with $EDITOR

Example entries

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 At 23:00‑07:00 every two hours and at 08:00: 0 23-7/2,8 * * * echo "Have a good dream" >> /tmp/test.txt On the 4th of each month and on Mondays‑Wednesdays at 11 am: 0 11 4 * 1-3 command January 1st at 4 am:

0 4 1 1 * command
Cron schedule illustration
Cron schedule illustration
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.

OperationsLinuxcroncrontab
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

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.