5

Different ways of writing crontab syntax

 2 years ago
source link: https://ma.ttias.be/different-ways-of-writing-crontab-syntax/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Different ways of writing crontab syntax

Mattias Geniar, April 26, 2020

Follow me on Twitter as @mattiasgeniar

Here’s what I’ve been doing for most of my life, when it comes to writing crontab entries:

0 */2 * 1 3 /run/this/on/wednesday.sh

The above is the arcane way of saying: run this job every 2 hours on every Wednesday in January.

But I only just recently learned you can replace the weekday and month with a human-readable form, if your cron version is recent enough.

0 */2 * JAN WED /run/this/on/wednesday.sh

It doesn’t help the 0 */2 ... syntax, but it’s a lot easier to see WED means Wednesday and JAN means January!

What else can you do in a crontab?

While some of these may be obvious if you’ve used crontab for a while, it is good to know the syntax allows for pretty flexible entries.

Ranges

This example shows you how to run a command ever hour between 8AM and 3PM, including those hours themselves.

0 8-15 * * * /script.sh

Lists

An alternate approach is to list the digits comma-separated.

0 8,9,10,11,12,13,14,15 * * * /script.sh

This would make sense if there’s a single hour you don’t want to run a command, you can simply leave it out of your comma-separated list.

Step values

This is the part of the crontab syntax that confuses most:

0 */2 * * * /script.sh

The */2 part can be read as: every 2 hours.

You can also pass a range with a step value:

0 0-12/2 * * * /script.sh

This one means: run this every 2 hours for all hours between 0 and 12.

Month and week names

Like I mentioned at the top, you can use the first 3 letters of a week or month to specify the entry.

0 0 * AUG SUN /script.sh

The above would run every Sunday in August at midnight.

Gotchas and booby traps

What does this Syntax do?

30 4 1,15 * 5 /script.sh

This would run a command on the 1st and 15th of each month, as well as on every Friday. The 1,15 in the day section and the 5 in the weekday are interpreted as an “either may match”, not a “should match both”.

Special shortcuts

Cron has some special shortcuts available to you, if you don’t want to write the arcane syntax every time.

string         meaning
------         -------
@reboot        Run once, at startup.
@yearly        Run once a year, "0 0 1 1 *".
@annually      (same as @yearly)
@monthly       Run once a month, "0 0 1 * *".
@weekly        Run once a week, "0 0 * * 0".
@daily         Run once a day, "0 0 * * *".
@midnight      (same as @daily)
@hourly        Run once an hour, "0 * * * *".

This means you can do this:

@daily /script.sh

instead of:

0 0 * * * /script.sh

At least @daily is more readable.

Any more tricks?

What other crontab magic am I missing here?


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK