What is a "cron job expression"?

This page will explain how to read and interpret the cron syntax found on Linux servers. This is useful information if you want to configure cron job monitoring in Oh Dear.

How to find the current cron jobs on your server #

Let's first explore how you can see the currently configured cron jobs on your server. To do this, you will need server access via SSH. If you don't have this, please talk to your hosting provider to get such access.

Once you're logged in via SSH, run the crontab -l command (that's a lower case L).

$ crontab -l
* * * * * php htdocs/artisan artisan schedule:run

Notice that line as the output of the crontab command? That's a scheduled task or "cron job".

Some important caveats to these cron jobs:

  • Each user on your Linux server can have different cron jobs. The crontab -l command will show the crons for your currently logged in user.
  • crontab -l shows the crons for a user, but system-wide cron jobs can also be configured in the directories /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly, /etc/cron.monthly and as a set of configuration files in /etc/cron.d.

Interpreting crontab syntax #

The syntax for cron jobs is as follows:

* * * * * {command-to-run}

Those 5 blocks of asterisks define the frequency, as follows:

# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of the month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of the week (0 - 6)
# │ │ │ │ │                (Sunday to Saturday;
# │ │ │ │ │                 7 is also Sunday on some systems)
# │ │ │ │ │
# * * * * * <command to execute>

Let's look at this example:

# ┌───────────── every 0'th minute (aka: start of hour)
# │  ┌───────────── on every hour that's divisible by 4
# │  │              (aka: every 4 hours)
# │  │  ┌───────────── on every day of the month
# │  │  │ ┌───────────── on every month
# │  │  │ │ ┌───────────── on every weekday
# │  │  │ │ │
# │  │  │ │ │
# │  │  │ │ │
# 0 */4 * * * logrotate

This command will execute every 4 hours, at every start of the hour. So at 00:00, 04:00, 08:00, ...

More commonly, you'll find a cronjob defined as this:

# ┌───────────── every minute
# │ ┌───────────── on every hour
# │ │ ┌───────────── on every day of the month
# │ │ │ ┌───────────── on every month
# │ │ │ │ ┌───────────── on every weekday
# │ │ │ │ │
# │ │ │ │ │
# │ │ │ │ │
# * * * * * logrotate

The * * * * * syntax means: run this command every minute.

What about sub-minute frequencies? #

By default, you can only scheduled commands to run every minute, not sooner. Crontab doesn't allow a faster frequency.

There are workarounds, like running a command that will invoke a different command, sleep a few seconds, then invoke it again, etc. For most use cases, users prefer "workers" that are miniature daemons that run on the system to perform commands.

Crontab isn't suitable for sub-minute frequency of scheduled tasks.

Using cron job expressions in Oh Dear #

When you want to monitor a cron job in Oh Dear, you can specify the frequency in minutes or as a cron job expression.

When you want to input the frequency as a cron job expression, you can copy the * * * * * syntax.

$ crontab -l
#       ┌─── This is the cron expression
#       │
#     │ │ │
#   │ │ │ │ │
# │ │ │ │ │ │ │
# 30 */2 0 0 */2   myscript.sh

In this complex example, the entire block 30 */2 0 0 */2 is the cron expression. It's everything before the actual command that should be run.

Was this page helpful to you? Feel free to reach out via support@ohdear.app or on Twitter via @OhDearApp if you have any other questions. We'd love to help!