Master Linux Scheduling: One‑Time and Recurring Tasks with at and crontab
This guide explains how Linux system administrators can automate one‑off and periodic jobs using the at command for single executions and the crond service with crontab for recurring tasks, providing concrete command examples, field syntax, and practical tips to avoid common pitfalls.
Why automate Linux tasks?
Experienced system administrators often need services or commands to start or stop automatically at specific times without manual intervention. While ad‑hoc scripts can handle batch work, relying on manual keystrokes (e.g., pressing Enter at 2 am) is inefficient and error‑prone.
One‑time tasks with at
A one‑off job runs only once, suitable for temporary needs. Use the at command followed by the desired time. Example to restart a web service at 23:30 tonight:
[root@linuxprobe ~]# at 23:30
at> systemctl restart httpd
at> <em>Press Ctrl+D to finish</em>
job 3 at Mon Apr 27 23:30:00 2015List pending one‑time jobs with at -l and delete a job with atrm <job_id>. A non‑interactive variant pipes the command into at:
[root@linuxprobe ~]# echo "systemctl restart httpd" | at 23:30
job 4 at Mon Apr 27 23:30:00 2015Recurring tasks with crond and crontab
For regular automation, the built‑in crond daemon is ideal. Edit the schedule with crontab -e, view it with crontab -l, and remove it with crontab -r. Administrators can manage other users’ crontabs using the -u option.
Each crontab line follows the five‑field format minute hour day month weekday command . Use an asterisk ( *) as a wildcard for any field.
Minute : 0‑59
Hour : 0‑23
Day : 1‑31
Month : 1‑12
Weekday : 0‑7 (0 and 7 both represent Sunday)
Command : the program or script to execute (use absolute paths)
Example: back up a website directory every Monday, Wednesday, and Friday at 03:25.
[root@linuxprobe ~]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
[root@linuxprobe ~]# crontab -l
25 3 * * 1,3,5 /usr/bin/tar -czvf backup.tar.gz /home/wwwrootField shortcuts:
Comma ( ,) separates multiple values (e.g., 1,3,12 for Jan, Mar, Dec).
Dash ( -) defines a range (e.g., 12-15 for days 12 through 15).
Slash ( /) specifies step intervals (e.g., */2 runs every 2 minutes).
Another practical job: clean /tmp every weekday at 01:00. First locate the absolute path of rm:
[root@linuxprobe ~]# whereis rm
rm: /usr/bin/rm /usr/share/man/man1/rm.1.gz ...
[root@linuxprobe ~]# crontab -e
crontab: installing new crontab
[root@linuxprobe ~]# crontab -l
25 3 * * 1,3,5 /usr/bin/tar -czvf backup.tar.gz /home/wwwroot
0 1 * * 1-5 /usr/bin/rm -rf /tmp/*Best practices and pitfalls
Comments can be added with # just like in shell scripts, helping future reviewers understand each job.
The “minute” field must contain a numeric value; it cannot be left empty or be * when other fields are specific.
Do not specify both “day of month” and “weekday” simultaneously, as they may conflict.
By following these guidelines, administrators can reliably delegate routine operations to the system, reducing manual effort and minimizing errors.
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.
