Master Linux Cron: Schedule Tasks Efficiently with Crontab
This guide explains Linux's cron daemon, its configuration files, permission controls, how to write and install crontab scripts, and provides numerous scheduling examples, helping administrators automate tasks efficiently, including command syntax, special characters, and common service commands.
Cron Service
crond is the daemon that runs scheduled tasks in Linux, similar to Windows Task Scheduler. It starts automatically after installation and checks every minute for tasks to execute.
Cron 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 statusCron Configuration Files
The directory /var/spool/cron/ stores each user's crontab file, named after the user (e.g., /var/spool/cron/tom). Typically each user has only one crontab file.
/etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# Example of job definition:
# * * * * * user-name command to be executedThe MAILTO variable defines where error output is sent; users often change it to their own email.
/etc/cron.d/ Directory
This directory holds additional crontab files or scripts.
Permissions
Cron permissions are controlled by /var/adm/cron/cron.allow and /var/adm/cron/cron.deny. The presence or absence of these files determines which users can use the crontab command.
Creating a Cron Script
1. Write a script file, e.g., crontest.cron, with the desired schedule and command.
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.
Crontab Usage
The crontab command installs, removes, or lists tables that drive the cron daemon. Each line in a crontab consists of six fields: minute, hour, day‑of‑month, month, day‑of‑week, and the command.
minute hour day-of-month month day-of-week command
Valid values: 00‑59 00‑23 01‑31 01‑12 0‑6 (0 is Sunday)Special characters *, /, -, and , define ranges, steps, and lists.
Common options: -l list, -r remove, -e edit.
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.txtAt 23:00‑07:00 every two hours and at 08:00:
0 23-7/2,8 * * * echo "Have a good dream" >> /tmp/test.txtOther examples include specific days, months, and custom commands.
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.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
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.
