Master Linux Cron: Essential Guide to Scheduling Tasks Efficiently
Learn how to use Linux's crond and crontab for automating periodic tasks, from basic concepts and time syntax to practical examples, command options, debugging tips, user restrictions, and best practices for reliable scheduling and maintenance.
1. Overview of Scheduled Tasks
What is crond
crond is the scheduler, similar to an alarm clock, executing commands at specified times.
It is used for periodic tasks such as nightly backups or enabling/disabling services at specific hours.
Two types of scheduled tasks
System‑level tasks (e.g., temporary file cleanup, log rotation) and user‑level tasks (e.g., time sync, configuration backup, database backup).
2. Time Management in Crontab
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 # 15:25 daily
27 15,16,17 * * root touch /tmp/d.txt # 15:27, 16:27, 17:27
28 15-19 * * root touch /tmp/e.txt # minutes 28 between 15:00‑19:00
30 10-20/5 * * root touch /tmp/f.txt # every 5 hours between 10‑20
*/7 * * * * root touch /tmp/g.txt # every 7 minutes
37 10,20,30 * 4 * echo $(date) >> /tmp/g.txt # specific daysAvoid using * for the minute field unless you really want the command to run every minute.
Time field examples
00 02 ls # every day at 02:00
00 02 1 ls # on the 1st of each month at 02:00
00 02 14 2 ls # February 14 each year 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 # 14th of each month or every Sunday at 02:00
00 02 14 2 7 ls # Feb 14 or any Sunday in February at 02:00
/10 02 ls # every 10 minutes during the 02:00 hour
* * ls # every minute
00 00 14 2 ls # Feb 14 at midnight
/5 ls # every 5 minutes
00 02 1,5,8 ls # Jan, May, Aug at 02:00
00 02 1-8 ls # days 1‑8 of each month at 02:00
0 21 ls # daily at 21:00
45 4 1,10,22 ls # 04:45 on the 1st, 10th, 22nd of each month
45 4 1-10 ls # 04:45 from the 1st to the 10th each month
3,15 8-11/2 ls # minutes 3 and 15 every 2 days between 08:00‑11:00
0 23-7/1 ls # hourly from 23:00 to 07:00
15 21 1-5 ls # weekdays at 21:15crontab 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 (e.g., root).
3. Practical Cron Jobs
Synchronize time every 5 minutes as root
# ntpdate time.windows.com >/dev/null
# crontab -e -u root
# add line:
*/5 * * * * ntpdate time.windows.com >/dev/nullBackup script executed at 15:00 daily
# mkdir /backup
# tar zcf $(date +%F)_$(hostname)_etc.tar.gz /etc
# find /backup -name "*.tar.gz" -mtime +3 -exec rm -f {} \;
# crontab -l
00 15 bash /root/back.sh >/dev/nullCron restrictions and debugging
Add user to /etc/cron.deny to block them.
Check /var/log/cron for execution logs.
Use absolute paths in commands.
Redirect output to /dev/null or a log file.
4. Debugging Tips
Increase task frequency (e.g., every minute) while testing.
Write script output to a dedicated log file.
Ensure commands use absolute paths.
Inspect /var/log/cron for results.
5. Benefits of Scheduled Tasks
Automated cleanup
Regular backups
Time synchronization
For more details, see the original article.
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.
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.
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.
