Cron Expression for Every 12 Hours
The cron expression for "every 12 hours", what each field means, and the next run times on your clock.
What is the cron expression for every 12 hours? The cron expression for every 12 hours is 0 */12 * * *. It runs twice a day, at 00:00 and 12:00, exactly like 0 0,12 * * *. To run at two other times 12 hours apart, name both hours: 0 8,20 * * * runs at 08:00 and 20:00 every day.
0 */12 * * *On the hour, every 12 hours
- …
- …
- …
- …
- …
How do you run every 12 hours at a specific time?
Name both hours in the hour field. Cron uses a 24-hour clock, so 8am and 8pm are 8 and 20: 0 8,20 * * *. For 6:30 in the morning and evening, set the minute too: 30 6,18 * * *. A range with a step, 0 8-23/12 * * *, gives the same two runs, but with only two values the list is easier to read.
The 24-hour clock is also where twice-daily schedules go wrong. 0 12 * * * is noon, not midnight, and 0 0 * * * is midnight. A line written as 0 12,24 * * * to mean noon and midnight is rejected, because the hour field only runs from 0 to 23.
Why does a date in a twice-daily backup command break the cron line?
In the command part of a crontab line, cron turns % into a newline and sends everything after the first % to the command as input. A backup line such as tar -czf /backups/site-$(date +%F).tar.gz /var/www is cut at the first %, so the shell receives a broken command and the backup fails. Escape each one as \% in the crontab, or put the command in a script and call the script.
The other thing to check on a 12-hour job is how long it runs. Cron starts the next run on the clock whether or not the last one has finished, so a backup that grows past 12 hours ends up running twice at once.
Frequently asked
Is 0 */12 * * * the same as 0 0,12 * * *?
Yes. Stepping by 12 through hours 0 to 23 matches only 0 and 12, so both run at midnight and noon. The list form is easier to edit later, for example to 0 1,13 * * * for 1am and 1pm.
How do I run a cron job every 12 hours starting at 9am?
Use 0 9,21 * * *. It runs at 09:00 and 21:00 every day. For a time like 9:30, change the minute field: 30 9,21 * * *.
Can cron run a job 12 hours after the last run finished?
No. Cron runs on clock times, not on gaps measured from the previous run. For that, use a systemd timer with OnUnitInactiveSec=12h, which counts from when the last run ended, or a worker that sleeps between runs.