Master Linux Local Mail, at/batch, and cron: A Practical Guide
This guide explains how to use Linux's local mail service and the at, batch, and cron commands to create, list, modify, and delete one‑time and recurring scheduled tasks, including command syntax, examples, and troubleshooting tips.
1. Introduction
Linux provides three task‑scheduling commands: at, batch, and cron. at and batch execute a job once, while cron runs jobs periodically; after a job finishes, the result is emailed to the submitter.
2. Local Mail Service
CentOS installs a local mail service that listens on port 25. You can send mail locally using the mail command. Two methods are shown:
1) Interactive entry: mail username (or username@hostname), then follow prompts for subject and body, finish with CTRL+D.
2) Using a file or pipe: mail -s "subject" user< file or echo "content" | mail -s "subject" user List received messages with mail and read a specific message by its number.
3. at / batch
Schedule a one‑time job with at now + count time‑units (units can be minutes, hours, days) or with an exact time such as hh:mm MMDDYY. Edit the job, finish with CTRL+D. Example screenshots show a task scheduled for a specific time.
View pending jobs with atq or at -l. Cancel a pending job with atrm jobID. The batch command works like at but runs automatically when the system is idle.
4. cron
The cron daemon runs continuously in the background. Verify it is active with service crond status; if it shows “stopped”, start it with service crond start. Enable it at boot with chkconfig crond on.
System‑wide crontab is /etc/crontab; user crontabs reside in /var/spool/cron and have the same format.
Crontab syntax: minute hour day month weekday command. Examples:
7 6 5 4 * /path/to/script # runs at 06:07 on April 5 each year */3 * * * * /bin/echo "hello" # every 3 minutes 20-40/3 * * * * /path/to/script # every 3 minutes between minute 20 and 40 each hourCommon crontab options: -l list jobs, -e edit (syntax checking is provided), -r remove all jobs. Administrators can edit a user's crontab with crontab -u USERNAME -e.
5. Summary
The article described how to create, modify, and delete scheduled tasks using at, batch, and cron, and how to use the local mail service to receive job results. It also reminded that in Linux everything is a file and that second‑level scheduling requires supplemental use of sleep.
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.
