cron & at
Schedule recurring and one-off jobs with crontab and at.
By Invicly Tech, , in Linux Administration (RHCSA-style). About 11 minutes to read.
In short
cron runs commands on a repeating schedule defined by five time fields, and at runs a command once at a given time. Users manage their own jobs with crontab -e, while system jobs go in /etc/cron.d. Output that is not redirected is mailed to the owner or lost, so log it deliberately.
Explained simply
Servers have chores: rotate logs, take backups, clean temp files, send reports. Doing them by hand is slow and easy to forget. cron is a background service that runs commands for you on a fixed schedule, such as every night at 02:30 or every 15 minutes.
Each scheduled job is one line in a table called a crontab. The line starts with five time fields (minute, hour, day of month, month, day of week) and ends with the command. A star in a field means every value.
at is cron's little sibling for one-time jobs. You tell it a time, such as 23:00 or now + 2 hours, and type the commands. It runs them once and forgets them. That is handy for a restart after business hours.
The big surprise for beginners: cron runs jobs with a tiny environment. Your PATH, aliases and shell settings are not there. Always use full paths, and send the output to a log file so you can see what happened.
Press Play on a diagram to walk through it one step at a time.
- Minute: 30
- Hour: 2
- 24-hour clock, so 02:30
- Day of month: *
- Month: *
- Day of week: 1-5
- Monday to Friday; 0 or 7 is Sunday
- Command
- Use absolute paths; cron has a minimal PATH
- 1crond wakes each minute
Reads crontabs that changed
- 2Time matches a line
30 2 * * * fits 02:30
- 3Fork as the job owner
/bin/sh, minimal PATH
- 4Run the command
% becomes a newline
- 5Handle output
Mailed to owner unless redirected
In depth
Reading the time fields like a pro
Minute is 0 to 59, hour 0 to 23, day of month 1 to 31, month 1 to 12 (or jan to dec), day of week 0 to 7, where both 0 and 7 mean Sunday. Each field takes lists (1,15), ranges (9-17) and steps (*/10 or 9-17/2).
One rule catches many people. If you restrict both day of month and day of week, cron runs the job when either one matches, not both. So 0 9 1 * 1 runs at 09:00 on the 1st of every month and also on every Monday. For the first Monday of a month, schedule every Monday and test the date inside the command.
Shortcuts exist too: @reboot, @hourly, @daily, @weekly, @monthly and @yearly.
- 0 */4 * * *: every 4 hours on the hour
- */5 9-17 * * 1-5: every 5 minutes in office hours on weekdays
- 0 0 1 1 *: once a year at midnight on 1 January
Where cron jobs live
Personal jobs go in your crontab, edited with crontab -e and stored under /var/spool/cron. They run as you and have no user field. System jobs go in /etc/crontab or files in /etc/cron.d/, and those lines have an extra user field after the time, such as root.
The folders /etc/cron.hourly, cron.daily, cron.weekly and cron.monthly hold plain scripts that run at those intervals. On RHEL, anacron runs the daily, weekly and monthly ones, so they still run after a machine was switched off at the scheduled time. On Debian and Ubuntu, run-parts runs these folders and silently skips files whose names contain a dot, so backup.sh is ignored while backup works.
- crontab -e
- Per user, no user field
- Stored in /var/spool/cron
- /etc/cron.d/NAME
- System jobs, user field required
- Best for packages and automation
- /etc/crontab
- System table, user field required
- /etc/cron.daily/ etc.
- Executable scripts, no schedule line
- anacron on RHEL; run-parts on Debian
The cron environment and output
cron starts each job with /bin/sh, a minimal PATH (usually /usr/bin:/bin), and your HOME and LOGNAME. Nothing from .bashrc is loaded. You can set variables at the top of a crontab, such as PATH=/usr/local/bin:/usr/bin:/bin, SHELL=/bin/bash and MAILTO=ops@example.com.
By default, any output is mailed to the job owner, which on most modern servers means it is lost because no mail system is set up. Redirect it on purpose: >> /var/log/job.log 2>&1, or pipe it to logger. Also remember that % is special in a crontab line: cron turns it into a newline. Write date +\%F, or put the command in a script.
- Logs of cron itself: /var/log/cron on RHEL, journalctl -u cron on Ubuntu
- The service is crond on RHEL and cron on Ubuntu
- cron.allow and cron.deny in /etc control who may use crontab
One-off jobs with at and batch
at reads commands from standard input or from a file with -f, and runs them once. It understands times like 16:30, 4pm tomorrow, now + 30 minutes, noon and midnight. The atd service must be running; on RHEL install the at package and run systemctl enable --now atd.
Unlike cron, at saves your current working directory and most of your environment with the job, so scripts often behave more like they do in your shell. atq lists waiting jobs, at -c NUMBER prints exactly what a job will run, and atrm NUMBER cancels it. batch is like at, but it waits until the system load is low, which suits heavy one-off tasks.
Making cron jobs safe
Three problems show up in real life. First, overlap: if a backup that runs every hour takes 70 minutes, two copies run at once and fight. Wrap the command in flock -n /run/lock/backup.lock so a second copy simply exits. Second, silent failure: nobody notices a job that stopped working months ago. Log the exit status, or ping a monitoring URL only on success. Third, accidents: crontab -r deletes your whole crontab with no question, and r sits next to e on the keyboard. Keep a copy with crontab -l > ~/crontab.bak.
Also watch time zones. cron uses the system time zone, and jobs between 01:00 and 03:00 may run twice or not at all on daylight saving days.
Learn it in 4 short steps
About 30m each. Track them with a timer in your plan.
- 1Read the five crontab time fields~30m
- 2Edit user crontabs with crontab -e~30m
- 3Use /etc/cron.d for system jobs~30m
- 4Schedule one-off jobs with at~30m
Key points
- */15 in the minute field means every 15 minutes.
- Files in /etc/cron.d include a user field before the command.
- crontab -l lists and crontab -u user -e edits another user's jobs as root.
- at now + 10 minutes schedules a one-off job; atq lists and atrm removes jobs.
Key terms
Tap a card to flip it. 0/4 known
Examples
crontab -e
# m h dom mon dow command
*/15 * * * * /usr/local/bin/check-disk.sh >> /var/log/check-disk.log 2>&1
30 2 * * 1-5 /usr/local/bin/backup.sh
sudo crontab -u natasha -l
echo "systemctl restart httpd" | sudo at 23:00
atqRecurring jobs with cron, a one-off restart with at.
A nightly database dump that does not overlap
You want a dump at 01:15 every night, kept for 7 days. Step 1: write the job in /etc/cron.d so it is part of the system config and runs as the postgres user. Step 2: set PATH in the file. Step 3: use flock so a slow run never overlaps the next one. Step 4: escape % in the date. Step 5: a second line at 01:45 deletes dumps older than 7 days. Check /var/log/dump.log the next morning.
sudo tee /etc/cron.d/pgdump <<'EOF'
PATH=/usr/local/bin:/usr/bin:/bin
15 1 * * * postgres flock -n /tmp/pgdump.lock pg_dumpall -f /backup/pg-$(date +\%F).sql >> /var/log/dump.log 2>&1
45 1 * * * postgres find /backup -name 'pg-*.sql' -mtime +7 -delete
EOF
sudo chmod 644 /etc/cron.d/pgdump
sudo touch /var/log/dump.log && sudo chown postgres /var/log/dump.logSchedule a restart after office hours with at
A config change needs an Apache restart, but not during the day. Step 1: queue the job for 22:00 with at. Step 2: check it is in the queue and read exactly what it will run. Step 3: if plans change, remove it. Step 4: the next day, look in the journal to confirm the restart happened.
echo 'systemctl restart httpd && logger -t atjob "httpd restarted"' | sudo at 22:00
sudo atq
# 7 Tue Nov 10 22:00:00 2026 a root
sudo at -c 7 | tail -3
# sudo atrm 7 # to cancel
journalctl -t atjob --since yesterdayQuick reference
- Fields: minute hour day-of-month month day-of-week, then the command
- Day of month and day of week both set means either may match
- crontab -l lists, -e edits, -r deletes everything without asking
- /etc/cron.d files need a user field: 0 3 * * * root /usr/local/bin/job
- Escape % as \% inside crontab lines
- flock -n /run/lock/job.lock cmd stops overlapping runs
- at now + 1 hour, atq to list, at -c N to inspect, atrm N to cancel
- Debian run-parts skips script names with dots in /etc/cron.daily
Common mistakes
- Relying on PATH or environment variables that cron does not set.
- Using an unescaped % in a crontab command, where cron treats it as a newline.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Add a personal cron job that writes the date and uptime to ~/uptime.log every 10 minutes. Confirm it ran.
- Exercise 2medium
As root, create a cron job for user natasha that runs /usr/bin/logger "hello" at 14:23 every day, and verify it from the logs.
- Exercise 3hard
Run a job at 02:00 on the first Monday of each month only.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Why is my cron job not running?
- Check that crond (RHEL) or cron (Ubuntu) is active, look in /var/log/cron or journalctl -u cron, and check the command with full paths. Most failures come from PATH, permissions or an unescaped %.
- What is the difference between cron and anacron?
- cron runs at exact times and skips jobs when the machine is off. anacron runs daily, weekly and monthly jobs once the machine is back up, so laptops and VMs that sleep still get them.
- How do I run a cron job every 30 seconds?
- cron's smallest step is one minute. Use a systemd timer with OnCalendar=*:*:0/30, or two cron lines where one starts with sleep 30.
- Should I use cron or systemd timers?
- Both work. Timers give better logging, catch-up after downtime and no overlap, while cron is simpler and found everywhere. On the RHCSA you may be asked for either.