Master Linux Cron: From Basics to Advanced Scheduling Techniques
This article provides a comprehensive guide to Linux cron jobs, covering fundamental concepts, time syntax, crontab commands, practical examples for system and user tasks, debugging tips, and best practices for writing reliable scheduled tasks.
1. Overview of Scheduled Tasks
1. What is crond
crond is the scheduler, similar to an alarm clock, executing commands at specified times.
Why use crond: for periodic tasks such as nightly backups or enabling/disabling services at specific times.
2. Types of scheduled tasks
System-level tasks: temporary file cleanup, system info collection, log rotation.
User-level tasks: time synchronization, configuration backup, database backup.
2. Cron Time Management
1. Crontab file syntax
/etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# minute hour day-of-month month day-of-week user command
22 * * * * root touch b.txt # every hour at minute 22
25 15 * * * root touch /tmp/c.txt # daily at 15:25
27 15,16,17 * * * root touch /tmp/d.txt # at 15:27,16:27,17:27 each day
28 15-19 * * * root touch /tmp/e.txt # minutes 28 between 15:00-19:00 each day
30 10-20/5 * * * root touch /tmp/f.txt # every 5 hours between 10 and 20
*/7 * * * * root touch /tmp/f.txt # every 7 minutes
37 * 10,20,30 * 4 root echo $(date) >> /tmp/g.txt # specific daysTip: avoid using * for minutes unless you really want every minute.
2. Crontab time field examples
00 02 ls # daily at 02:00
00 02 1 ls # monthly on the 1st at 02:00
00 02 14 2 ls # yearly on Feb 14 at 02:00
00 02 7 ls # every Sunday at 02:00
00 02 6 5 ls # every Friday in June at 02:00
00 02 14 7 ls # on the 14th of each month or every Sunday at 02:00
/10 02 ls # every 10 minutes at 02:00
* * * * * ls # every minute
00 00 14 2 ls # yearly on Feb 14 at midnight
/5 ls # every 5 minutes
00 02 1,5,8 ls # yearly on Jan, May, Aug at 02:00
00 02 1-8 ls # daily from the 1st to 8th at 02:00
0 21 ls # daily at 21:00
45 4 1,10,22 ls # specific dates at 04:45
3,15 8-11/2 ls # every 2 days between 8-11 at minutes 3 and 15
0 23-7/1 ls # hourly between 23:00 and 07:00
15 21 1-5 ls # weekdays at 21:153. Crontab command options
-e : edit scheduled tasks
-l : list scheduled tasks
-r : remove scheduled tasks
-u : specify another user
Default crontab files are stored in /var/spool/cron/username.
3. Practical Cron Examples
1. Sync time every 5 minutes as root
# Sync time
ntpdate time.windows.com &>/dev/null
# Add cron job
crontab -e -u root
# Example entry
*/5 * * * * root ntpdate time.windows.com &>/dev/null2. Backup script executed at 15:00 and 17:00 every day
Create backup script that creates a tarball and removes files older than 3 days.
Save script as /root/back.sh.
Add cron entry:
0 15,17 * * * root bash /root/back.sh &>/dev/null4. Cron precautions
Add comments to each job.
Put commands in shell scripts instead of inline.
Redirect output to /dev/null or a log file.
Ensure commands are executable; use absolute paths.
5. Restrict a user from using cron
# Add user to /etc/cron.deny
echo "lqz" >> /etc/cron.deny
# Test as the user
crontab -e # will be denied4. Debugging Cron Jobs
1. Debugging tips
Increase frequency (e.g., every minute) while testing.
Write script output to a log file.
Use absolute paths for commands.
Check /var/log/cron for execution results.
2. Writing process
Manually run the command and verify success.
Encapsulate the command in a script located in a consistent directory.
Make the script executable and use bash to run it.
Add the script command to crontab with proper comments and schedule.
Test and adjust frequency, environment variables, and logs.
5. Uses of Scheduled Tasks
Automatic deletion of files.
Regular backups.
Time synchronization with ntpdate.
View cron logs with tail /var/log/cron.
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.
Raymond Ops
Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.
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.
