What is a cron job?
A cron job consists of two parts: a schedule and a command. The schedule defines when something happens. The command defines what happens, usually a call to a script or program.
The key terms:
- Cron: the service (daemon) that runs in the background and starts the tasks.
- Crontab: the table that holds the tasks. Every user has their own crontab, and there is one for the system as a whole.
- Cron job: a single line in the crontab, in other words one scheduled task.
- Cron expression: the notation for the schedule, for example
0 3 * * *for “daily at 3 am”.
“Cron job” and “cronjob” mean the same thing. English usually writes two words; German often writes one.
How does cron work?
The cron service starts with the server and checks once a minute whether any crontab contains a task for that minute. If so, it runs the command with the permissions of the user who owns the crontab.
Three properties are worth knowing:
- The smallest unit is the minute. Cron cannot schedule tasks by the second.
- Cron does not catch up. If the server was switched off at the scheduled time, the task is skipped.
- Cron does not know your login environment. Commands run with a very small environment. This is the most common reason why a script works by hand but not as a cron job.
Cron syntax: five fields and a command
Every line of the crontab has the same structure: five fields for the time, separated by spaces, followed by the command.
minute hour day month weekday command
- Minute: 0 to 59
- Hour: 0 to 23
- Day of the month: 1 to 31
- Month: 1 to 12 or jan to dec
- Day of the week: 0 to 7 or sun to sat, where 0 and 7 both stand for Sunday
These characters are allowed in every field:
*stands for every value.,separates several values:1,15in the day field means on the 1st and the 15th.-defines a range:1-5in the weekday field means Monday to Friday./defines steps:*/15in the minute field means every 15 minutes.
One special rule often causes surprises: if both the day of the month and the day of the week are restricted, the task runs as soon as either matches. 0 8 13 * 5 therefore does not mean “Friday the 13th” but “on every 13th and on every Friday”.
There are shortcuts for common schedules: @hourly, @daily, @weekly, @monthly, @yearly and @reboot for “once after the server starts”.
Cron expression examples
The following expressions cover most cases and can be copied directly:
* * * * *every minute*/5 * * * *every 5 minutes0 * * * *on the hour, every hour30 2 * * *daily at 2.30 am0 8,18 * * *daily at 8 am and 6 pm0 7 * * 1-5Monday to Friday at 7 am0 6 * * 1every Monday at 6 am0 0 1 * *on the first of every month at midnight0 9 1 1,4,7,10 *on the first day of every quarter at 9 am0 22 * * 0every Sunday at 10 pm
A complete line including the command looks like this:
30 2 * * * /usr/bin/php /var/www/shop/bin/feed-export.php >> /var/log/feed-export.log 2>&1
It starts a PHP script every night at 2.30 am and writes output and error messages to a log file.
Checking a cron expression: the cron generator
Enter a cron expression or choose an example. The tool explains each of the five fields and shows the next five runs in your local time. This lets you see whether the schedule is right before you add it to the crontab.
The tool supports the standard five-field syntax with *, lists, ranges, steps, names for months and weekdays, and shortcuts such as @daily. Seconds, years and special characters of other systems such as L, W or # are not supported.
Setting up a cron job step by step
On a server with command line access, you set up a cron job in four steps:
- Open the crontab: the command
crontab -eopens the crontab of the logged-in user in a text editor. - Add a line: write the schedule and the command on a new line. Use full paths for programs and files.
- Save and close: cron picks up the change immediately. No restart is needed.
- Check:
crontab -llists all tasks. Test the task first with a time a few minutes in the future.
Be careful with crontab -r: the command deletes the entire crontab without asking.
Besides the user crontabs there are the system files /etc/crontab and the directory /etc/cron.d. There, the user under which the task runs is given between the schedule and the command.
Without a command line: with most hosting packages you create cron jobs in the control panel, for example in Plesk or cPanel. You select the schedule there and enter the command or the address of a script. The logic behind it is the same.
What are cron jobs used for when running a website?
Almost every larger website and every shop uses cron jobs, often without anyone noticing. Typical tasks:
- Backups of the database and files
- Product data: exporting and updating feeds for Google Shopping, price comparison sites and affiliate networks; see feed optimisation
- XML sitemaps: regenerating them
- Caches: clearing and warming them
- Imports: synchronising stock levels and prices from the inventory system
- Reports: retrieving data via an API and sending it as a report
- Housekeeping: rotating or deleting old sessions, temporary files and log files
- Emails: sending newsletters and notifications from a queue
This matters more for SEO than it sounds. If the cron job for the feed fails, Google shows outdated prices. If the sitemap is not regenerated, Google finds new pages later. A heavy cron job at peak time can slow the server and increase loading time. Tasks like these should run at night.
WordPress: WP-Cron and a real cron job
WordPress has its own system for scheduled tasks, WP-Cron. It publishes scheduled posts, checks for updates and runs tasks for plugins. However, WP-Cron is not a real cron job: it is not triggered by the clock but by page views. On every visit, WordPress checks whether tasks are due.
This has two drawbacks:
- Few visitors: tasks run late or not at all. A post scheduled for 6 am only appears with the first page view after that.
- Many visitors: the check runs with every request and costs performance. A page cache can also prevent WP-Cron from being triggered at all.
The solution is a real cron job. To do this, switch off the built-in trigger in the file wp-config.php:
define('DISABLE_WP_CRON', true);
A cron job on the server then calls the file wp-cron.php regularly, for example every five minutes:
*/5 * * * * wget -q -O /dev/null "https://www.example.com/wp-cron.php?doing_wp_cron"
Settings like these are part of our WordPress SEO work.
Troubleshooting: why is my cron job not running?
If a cron job does not run, or runs differently from what you expect, it is almost always down to one of these causes:
- Missing paths: cron only knows a few directories. Write programs with their full path, so
/usr/bin/phpinstead ofphp, and do not use relative paths in the script either. - Missing permissions: the script must be executable for the user of the crontab, and that user must be allowed to write to the target directories.
- Wrong time zone: cron follows the clock of the server. If it is set to UTC, a task scheduled for 8 am runs at 8 am UTC, not at your local time.
- The percent sign: in the crontab,
%stands for a line break. In commands such asdate +%Fit must be written with a backslash:\%. - No line break at the end: the last line must end with a line break. Without it, some versions of cron ignore the line or reject the crontab.
- No output: without redirection, error messages are lost or end up in a mailbox nobody reads. Redirect the output to a file:
>> /var/log/task.log 2>&1. - Overlapping runs: if a task takes longer than its interval, several copies run at the same time. The program
flockprevents this.
The system log shows whether cron started the task at all. Depending on the system, grep CRON /var/log/syslog or journalctl -u cron helps. systemctl status cron shows whether the service is running; on some systems it is called crond.
Alternatives to cron
Cron has been the standard for decades, but it is not always the best choice:
- systemd timers: available on modern Linux systems. They can catch up on missed runs, make tasks depend on one another and log by themselves.
- anacron: for machines that do not run continuously. Missed daily or weekly tasks are caught up.
- Windows Task Scheduler: the counterpart on Windows servers.
- Cloud services: such as Cloud Scheduler at Google, EventBridge at AWS or CronJobs in Kubernetes. They use the same notation or a very similar one. EventBridge, for example, works with six fields, the sixth being the year.
- External cron services: they call an address on your website at fixed intervals. Useful when the hosting does not allow cron jobs of its own.
You will also meet cron notation outside servers, for example in scheduled workflows in GitHub Actions or scheduled queries in databases. Once you have understood the five fields, you can read them anywhere.
Is your feed, your sitemap or your WordPress scheduling not running reliably? Then get in touch with us or read more about our technical SEO!
