Operations 9 min read

Mastering Linux Cron: How to Schedule Tasks Efficiently

This guide explains what the Linux cron service is, how to configure it via /var/spool/cron and /etc/crontab, the syntax of cron expressions, environment variables, and provides practical examples for scheduling recurring tasks.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Mastering Linux Cron: How to Schedule Tasks Efficiently

1) Introduction to cron

Cron is a Linux program that automatically executes specified tasks at scheduled times. The service can be started or stopped using the /etc/init.d/crond script.

2) Configuring cron

Cron can be configured in two ways: editing files in the /var/spool/cron directory or editing the /etc/crontab file.

a) /var/spool/cron files

Use the crontab command to edit these files, or edit them directly. For example, the root user's cron file is /var/spool/cron/root. Commands: cat /var/spool/cron/root To edit, list, or remove the file:

crontab -e -u root   # edit
crontab -l -u root   # list
crontab -r -u root   # remove

b) /etc/crontab

Only the root user can edit /etc/crontab. Its default content includes environment variables and scheduled jobs:

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

Example entries:

01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly

The first four lines set the execution environment for cron jobs. SHELL defines the shell, PATH defines command search paths, MAILTO receives job output, and HOME sets the working directory. Custom variables can also be defined, e.g.:

NAME="Chenkuo"
*/1 * * * * root /bin/echo "$NAME" > /tmp/2

After modifying /etc/crontab, restart the cron service with /etc/init.d/crond restart to apply changes.

3) Cron runs with a minimum unit of one minute

Cron checks configuration files every minute, so it cannot schedule tasks more frequently than once per minute.

4) Cron expression syntax

Fields are separated by spaces: * * * * * command representing minute, hour, day of month, month, day of week, and the command. Special characters:

* – any value

- – range (e.g., 1-4)

, – list (e.g., 3,4,6,8)

/ – step (e.g., 0-59/2 for every two minutes, */3 for every three months)

Note that /etc/crontab allows specifying the user to run the command, unlike user‑specific crontabs.

5) Cron examples

Various scheduling examples:

30 21 * * * /etc/init.d/httpd restart          # every night at 21:30
45 4 1,10,22 * * /etc/init.d/httpd restart      # on the 1st, 10th, 22nd of each month at 04:45
10 1 * * 6,0 /etc/init.d/httpd restart          # every Saturday and Sunday at 01:10
0,30 18-23 * * * /etc/init.d/httpd restart      # every 30 minutes between 18:00 and 23:00 daily
0 23 * * 6 /usr/local/etc/rc.d/lighttpd restart # every Saturday at 23:00
* */1 * * * /etc/init.d/httpd restart          # hourly
* 23-7/1 * * * /etc/init.d/httpd restart       # every hour between 23:00 and 07:00
0 11 4 * mon-wed /etc/init.d/httpd restart     # on the 4th of each month and Monday‑Wednesday at 11:00
0 4 1 jan * /etc/init.d/httpd restart          # on January 1st at 04:00
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.

Automationtask schedulingcron
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.