Server Administration
Cron Jobs Guide: Automating Server Tasks on Linux
By NileVPS Team · Published · 5 min read
اقرأ المقال بالعربيA server that needs you to log in every night to run a backup, clear a cache or renew something is a server waiting to be forgotten. Cron is the classic Linux scheduler that runs commands automatically at set times, and it is available on practically every distribution. Used well, it handles backups, reports, cleanups and application queues without human attention. Used carelessly, it fails silently for months. This guide covers the syntax, practical examples and the habits that make cron jobs reliable.
How cron works
The cron daemon (cron on Debian and Ubuntu, crond on RHEL-family systems such as AlmaLinux and Rocky) wakes up every minute, reads the schedules it knows about and runs any job whose time has come. Schedules live in a few places:
- User crontabs, edited with
crontab -e, stored under/var/spool/cron/. Jobs run as that user. - `/etc/crontab` and files in `/etc/cron.d/`, which include an extra user field.
- `/etc/cron.daily/`, `cron.weekly/`, `cron.monthly/`, folders of scripts run on a fixed schedule.
Useful commands:
crontab -e # edit your crontab
crontab -l # list it
crontab -u www-data -l # list another user's crontab (as root)
systemctl status cron # check the daemon (crond on RHEL family)Crontab syntax
Each line has five time fields followed by the command:
# ┌ minute (0-59)
# │ ┌ hour (0-23)
# │ │ ┌ day of month (1-31)
# │ │ │ ┌ month (1-12)
# │ │ │ │ ┌ day of week (0-7, 0 and 7 are Sunday)
# │ │ │ │ │
* * * * * command| Symbol | Meaning | Example |
|---|---|---|
* | every value | * * * * * every minute |
, | list | 0 8,20 * * * at 08:00 and 20:00 |
- | range | 0 9-17 * * 1-5 hourly, 9 to 5, weekdays |
/ | step | */15 * * * * every 15 minutes |
Shortcuts such as @reboot, @hourly, @daily, @weekly and @monthly are also supported by common cron implementations.
One subtle rule: if both day-of-month and day-of-week are restricted, the job runs when either matches, not both.
Practical examples
# nightly backup at 02:30
30 2 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1
# Laravel scheduler every minute
* * * * * cd /var/www/app && php artisan schedule:run >> /dev/null 2>&1
# WordPress cron, after setting DISABLE_WP_CRON in wp-config.php
*/5 * * * * cd /var/www/site && wp cron event run --due-now --quiet
# delete temp files older than 7 days, Sundays at 04:00
0 4 * * 0 find /var/www/app/tmp -type f -mtime +7 -delete
# start a bot after reboot
@reboot /home/bot/start.shEnvironment: the most common source of failures
Cron runs commands with a minimal environment. Your interactive shell's PATH, aliases and variables are not loaded, and the default shell is /bin/sh. A script that works in your terminal can fail under cron for that reason alone.
- Use absolute paths:
/usr/bin/php, notphp. Find them withcommand -v php. - Set variables at the top of the crontab:
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
MAILTO=""- Escape percent signs. In a crontab line
%means newline, so writedate +\%F. - Remember the server timezone. Check it with
timedatectl; cron uses system time.
Logging and alerts
By default, cron tries to email any output to the job owner, which usually goes nowhere on a fresh VPS. Redirect output to a log file with >> /path/file.log 2>&1 so you can see what happened. Check the daemon's own records with journalctl -u cron (or -u crond), or grep CRON /var/log/syslog on Debian and Ubuntu. Our server logs guide explains these tools in more depth.
For important jobs, add a heartbeat: at the end of a successful run, the script calls a monitoring URL with curl -fsS. If the ping does not arrive on time, the monitoring service alerts you. This turns silent failures into visible ones, as described in our article on uptime monitoring.
Prevent overlapping runs
If a job sometimes takes longer than its interval, two copies can run at once and corrupt data. Wrap it in flock:
*/10 * * * * /usr/bin/flock -n /tmp/sync.lock /usr/local/bin/sync.shWith -n, the new run exits immediately if the previous one still holds the lock. For heavy jobs, also lower their priority with nice -n 10 ionice -c3 so they do not slow down your website.
Cron or systemd timers?
On systemd-based distributions, timers are a strong alternative. They log to the journal automatically, can catch up on missed runs with Persistent=true, and support randomized delays. Cron stays simpler for quick tasks and is understood everywhere. Both are valid; pick one style per server and document it.
Checklist for reliable cron jobs
- [ ] Absolute paths for commands and files
- [ ] Output redirected to a log file
- [ ]
flockon jobs that could overlap - [ ] Timezone confirmed with
timedatectl - [ ] Heartbeat or alert on critical jobs
- [ ] Crontabs included in your backups
FAQ
Why does my script work manually but not in cron?
Almost always because of the environment. Cron uses a short PATH, /bin/sh as the shell and a different working directory. Use absolute paths, cd into the right folder, set SHELL and PATH in the crontab, and log output to see the actual error.
How do I run a job every 30 seconds?
Cron's smallest unit is one minute. You can add two entries, one with sleep 30; before the command, or use a systemd timer, which supports second-level precision. For constant work, a long-running worker managed by systemd is usually a better design.
Where can I see if a cron job ran?
Check journalctl -u cron or /var/log/syslog on Debian and Ubuntu, and journalctl -u crond or /var/log/cron on the RHEL family. These show that the job started; your own log file shows whether it succeeded.