Master Linux Cron: Schedule Tasks, Manage Services, and Write Crontabs
This guide explains what the Linux cron daemon does, how to control the crond service, where cron configuration files reside, permission handling, creating cron scripts, crontab syntax, and provides numerous practical examples for automating system tasks.
What Is Cron?
Cron is a Linux daemon that runs scheduled jobs without human intervention, similar to Windows Task Scheduler. After the OS installation, the crond service starts automatically and checks every minute for tasks to execute.
Cron Service Control
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 // view service statusConfiguration Files
/var/spool/cron/ stores each user’s personal crontab file (e.g., /var/spool/cron/tom for user tom). Typically each user has only one file.
/etc/crontab is a system‑wide crontab managed by the administrator. It includes environment variables and the job format:
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) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# * * * * * user-name command to be executedThe MAILTO=root variable determines where error output is sent; many users change it to their own email address to monitor failures.
/etc/cron.d/ holds additional crontab files or scripts that the system will execute.
Permission Files
Cron permissions are controlled by cron.allow and cron.deny under /var/adm/cron/ (or similar paths). The rules are:
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.
If cron.allow is missing but cron.deny exists, all users except those listed in cron.deny may use crontab.
If both exist, a user must be present in cron.allow and absent from cron.deny to be allowed.
Creating a Cron Script
1. Write a script file, e.g., crontest.cron:
15,30,45,59 * * * * echo "xgmtest....." >> xgmtest.txt # runs every 15 minutes2. Install it with crontab crontest.cron.
3. Verify using crontab -l or by checking /var/spool/cron for the generated file.
Note: This replaces the current user’s crontab rather than appending.
Using the crontab Command
The crontab utility installs, removes, or lists tables that drive the cron daemon. Each user has a separate 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 commandValid ranges are 00‑59 for minutes, 00‑23 for hours, 01‑31 for day‑of‑month, 01‑12 for month, and 0‑6 for day‑of‑week (0 = Sunday). Special characters: * – all possible values / – step values (e.g., */5 every 5 units) - – range (e.g., 1-5) , – list of discrete values
Common crontab options:
-l // list current crontab
-r // remove current crontab
-e // edit crontab using $VISUAL or $EDITORPractical Examples
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.txtFrom 23:00 to 07:00 every two hours and at 08:00:
0 23-7/2,8 * * * echo "Have a good dream" >> /tmp/test.txtOn the 4th of each month and Monday‑Wednesday at 11 am: 0 11 4 * 1-3 command January 1st at 4 am: 0 4 1 1 * command System‑wide directories such as /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly, and /etc/cron.monthly are executed by the corresponding entries in /etc/crontab (e.g., 01 * * * * root run-parts /etc/cron.hourly).
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
