A Beginner's Guide To Cron Jobs

By XanoJanuary 31, 2022

Cron is among the most popular job schedulers and can be an incredibly useful tool for automating repetitive tasks and recurring jobs. Cron can be a massive timesaver that is relatively easy for even novice programmers to learn. Considered an integral part of Unix-like operating systems, you will likely have to learn the basics of running Cron jobs at some point if you work in tech.

Below, we will go over the basic syntax of Cron jobs and show you how to schedule commands. While Cron jobs may seem complicated at first glance, it is highly user-friendly. As long as you have a fundamental understanding of the syntax, it is easy to start using cron jobs regularly to schedule and automate various tasks.

What Is A Cron Job?

Cron is a time-based job scheduler. Using cron, users can schedule tasks repeatedly for specific times. You could schedule weekly or monthly backups, automate system maintenance, monitor disk space, and more. A cron job refers to any task scheduled using the cron utility.

Cron Job Syntax: The Basics

The basic syntax for a cron job is “* * * * * command/script,” with each asterisk representing a specific point in time:

  • The first asterisk corresponds to minutes and can be any number between 0 and 59
  • The second asterisk corresponds to hours and can be any number between 0 to 23
  • The third asterisk corresponds to the day of the month and can be any number between 1 and 31
  • The fourth asterisk corresponds to the month of the year and can be any number between 1 and 12
  • The fifth asterisk corresponds to the day of the week and can be any number from 0 to 6, with 0 being Sunday and 6 being Saturday

When specifying multiple values, you use the following symbols:

  • For all values in a field, use an asterisk (*)
  • For a range of values, use a dash (-)
  • For a list of values, use a comma (,)
  • For a step value, use a separator (/)

Cron Job Examples

On the most basic level, you can run a command every minute of every day by writing:

* * * * * [COMMAND]

You can specify further by adjusting your syntax. For example, say you want to run a command at the beginning of every hour. You would write:

            0 * * * * [COMMAND]

You can also specify a task to run once a day. Maybe you want to back up your files at 3 AM every day, for example. You would write:

            0 3 * * * [COMMAND]

What if you need to specify further and run a task at 3 AM on the second of every month? You would write:

            0 3 2 * * [COMMAND]

What if you only need that task done during the fall between October and December? You would write:

            0 3 2 10-12 * [COMMAND]

Maybe you need a task done at 3 AM every Sunday. You would write:

            0 3 * * 0 [COMMAND]

Or, maybe, you want a task to run every hour on Sundays. You would write:

            0 * * * 0 [COMMAND]

You probably get the idea by now. You can get highly specific about when to run a task by manipulating the figures you input. These are very rudimentary examples, and cron job tasks are typically more complex than what we have provided. However, this should give you a starting point for using cron jobs by showing you the basics of how they work.

Special Strings

Special strings are shortcuts you can use in cron jobs. They allow you to schedule jobs at specific time intervals without figuring out the logical set of numbers to input. Some commonly used strings include:

  • @hourly: A job runs once an hour
  • @daily or @ midnight: Runs tasks every day at midnight
  • @weekly: Runs a task once a week
  • @monthly: Runs a task once at the first of every month
  • @yearly: Runs a task every year at midnight on January 1

The Bottom Line

While the above guide only provides the basics, it should help you understand the fundamentals of scheduling cron jobs. Once you know the basic syntax of cron jobs, scheduling tasks is a fairly intuitive process, and there are plenty of special strings and shortcuts to help simplify things further. Most programmers can pick up cron quickly with a bit of practice.

Looking for solutions for your company? Xano is the fastest No Code Backend development platform on the market. We give you a scalable server, a flexible database, and a No code API builder that can transform, filter, and integrate with data from anywhere. Sign up here to get started.

The post A Beginner’s Guide To Cron Jobs appeared first on Xano.