Operations 12 min read

Master Linux Cron: Configure, Manage, and Schedule Tasks Efficiently

This guide explains what the cron daemon does on Linux, how to control the service, where its configuration files reside, permission handling, creating cron scripts, crontab syntax, and provides numerous practical scheduling examples for system administrators.

ITPUB
ITPUB
ITPUB
Master Linux Cron: Configure, Manage, and Schedule Tasks Efficiently

What is cron?

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

Controlling the cron service

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

Key configuration files

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

/etc/crontab is used by the system administrator to define system‑wide jobs. It contains environment variables such as:

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

The file also shows the classic five‑field schedule syntax:

# ┌─ minute (0‑59)
# │ ┌─ hour (0‑23)
# │ │ ┌─ day of month (1‑31)
# │ │ │ ┌─ month (1‑12) or jan,feb,...
# │ │ │ │ ┌─ day of week (0‑6, Sun=0) or sun,mon,...
# │ │ │ │ │
* * * * * user-name command

Permission control

Permission is governed by /var/adm/cron/cron.allow and /var/adm/cron/cron.deny:

If neither file exists, only root can use crontab.

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

If cron.deny exists (and cron.allow does not), 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.

Creating a cron script

Write a file (e.g., crontest.cron) with the desired schedule line:

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

Install it with crontab crontest.cron, then verify using crontab -l or by checking /var/spool/cron.

Using the crontab command

crontab

installs, removes, or lists tables that drive the cron daemon. Each line consists of six fields: minute, hour, day‑of‑month, month, day‑of‑week, and the command to run. Fields are separated by spaces or tabs.

minute hour day‑of‑month month day‑of‑week command
Valid ranges: 00‑59 00‑23 01‑31 01‑12 0‑6 (0 = Sunday)

Special characters: * – every possible value / – step values (e.g., */5 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 examples

Every day at 06:00: 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 On the 4th of each month at 11:00 on Monday‑Wednesday: 0 11 4 * 1-3 command January 1st at 04:00: 0 4 1 1 * command Run all scripts in /etc/cron.hourly at minute 01 of every hour: 01 * * * * root run-parts /etc/cron.hourly Run daily scripts at 04:02: 02 4 * * * root run-parts /etc/cron.daily Run weekly scripts on Sundays at 04:22: 22 4 * * 0 root run-parts /etc/cron.weekly Run monthly scripts on the 1st at 04:42:

42 4 1 * * root run-parts /etc/cron.monthly

Additional notes

The run-parts command executes every executable file in the specified directory; omitting it allows you to specify a single script instead of a directory.

When a cron job produces output, cron emails it to the address defined by MAILTO (default root), so redirecting output to a file is common practice.

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.

Schedulingcroncrontab
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

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.