How to Run Cron Jobs Every 5, 10, or 15 Minutes

Beginners-Guide-to-Cron-Jobs-Thumbnail-No-WM.png

In the dynamic world of software development, repetitive tasks can consume a significant amount of time and hinder productivity. By mastering the art of setting up cron jobs every 5, 10, or 15 minutes, developers can alleviate this pain point and enable themselves to focus on more valuable work. Dive into this comprehensive guide to learn how to automate and optimize your workflow using cron jobs like top industry professionals.

Related reading: Top 10 uses of a Worker system

Table of Contents

An Overview of Cron Jobs

Before you go about using cron jobs to schedule your tasks, you should first have an understanding of all the terms you'll need to know in order to run cron jobs. Fortunately, it's nothing too complex. You'll have your cron jobs up and running in no time.

What Is a Cron Job?

Simply put, a cron job is a task that is executed automatically at a certain specified interval. Theoretically, you could schedule a cron every 5 minutes, every week, every month, or every year — it's truly up to you and your specific needs how you use the scheduler. The cron jobs are driven by a crontab (cron table) file, a configuration file that specifies shell scripts to run periodically in your cron schedule. While there's no one designated use for a cron job, they're especially useful when it comes to sending emails, backing up data and databases, checking for available disc space, updating security patches to make sure they're always up to date, and any other task that relates to system maintenance or administrative duties.

Use cases for cron are varied and diverse and include RSS feeds, backups of databases and operating systems, and automation of ubuntu tasks.

With the GNU Project and Linux, new cron expressions and Linux commands appeared. Other implementations include anacron and dcron.

What Is a Crontab?

A crontab file — also known as a cron table — is the text file that allows you to define the schedule of your cron job. Typically, crontabs are created, viewed, edited, and deleted using the crontab command. Crontabs always have five fields, and each field is always represented by an asterisk. Each one of these fields will determine the date, the time, and the frequency of your repeating cron job.

Your first field will be minutes, and this will be how you determine the minute of the hour your cronjob will be running on. Possible inputs range from 0 to 59. Your second field is for the hour your cronjob will run, with possible inputs ranging from 0 to 23. The third is for a day of the month, ranging from 1 to 31. Fourth is the month, ranging from 1 to 12. Last is a day of the week, ranging from 0 to 7.

Additionally, you need to be familiar with the variety of characters present in each crontab file.

  • An asterisk (*) in any of the five fields will mean all allowed values. For example, an asterisk in the minute field will make the cron job repeat every minute.
  • A comma (,) will allow you to list values in repetition within a single field. For example, listing2,4,6 in the hour field will make the cron job repeat at 2, 4, and 6 a.m.
  • Hyphens (-) are best used when you need to input a range into a field. For instance, 0-3 in the day field would have your task running from Sunday to Wednesday.
  • A slash (/) serves a similar purpose to commas and hyphens by allowing you to indicate step values within a range. 0-6/2is the same as saying 0,2,4,6while */5would mean cron every 5 minutes, hours, days, etc.
  • The letter L indicates Last and can be used to indicate the last day of the month. For example, 1L would mean the last Monday.
  • The letter W, similar to the letter L, can be used to indicate the closest weekday to a specific time. This is useful when something needs to be done on the 1st of each month, but the first falls on a weekend — 1Wwould mean that the task would wait until Monday to run.
  • The hash symbol (#) is for determining a particular day of the week, like the third Friday of the month (3#5) or the first Monday (1#1).
  • The question mark (?) is for when you need to leave a space blank.

Related reading: Super Easy Batch Processing with Docker and IronWorker

Replace Your Cron Jobs With IronWorker

The scheduling capability within IronWorker is simple to use and offers greater reliability than cron jobs. Schedule your tasks for specific times, set a frequency/delay timer for task repetition, or enable tasks to be process on demand.

How to Run Cron Jobs

Use Cases Cron JobsNow that you've got an understanding of what a cron job is and how to define a cron job's schedule using a crontab, let's go over how to run cron jobs every 5, 10, or 15 minutes.

Every 5 Minutes

You actually have a couple of different ways you could run a cron job every five minutes: With commas or with an asterisk and a slash. Whether you prefer to write out 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55 or simply */5, either option will allow you to run your cron job every five minutes.

Every 10 Minutes

The same goes for every 10 minutes. You could either write out 0, 10, 20, 30, 40, 50, or you could write */10. Of course, it needs to be said that the former option leaves a lot more room for error than the latter (not to mention, it's much more time-consuming). You are probably better off sticking with the asterisk and slash, but it's really up to you.

Every 15 Minutes

If you've got the hang of things by now, then you know that you can run a cron job every 15 minutes by writing out 0, 15, 30, 45 or simply */15 in the crontab space for minutes.

Interested in learning more about cloud computing and serverless? Read our article: When to Know You Are Ready to go Serverless

cron jobs iron

Conclusion

While the process of setting up a cron job might have sounded difficult or intimidating at first, our hope is that you now have a firm grasp on cron jobs and can begin using them to create a more efficient work environment almost immediately. However, running your cron jobs in the cloud is a problem for developers. Learn more about IronWorker and its ability to run tasks in the cloud.

Schedule Cron Jobs In The Cloud

With IronWorker, you can schedule your tasks for specific times, set a frequency/delay timer for task repetition, or enable tasks to be process on demand.

blank

About Korak Bhaduri

Korak Bhaduri, Director of Operations at Iron.io, has been on a continuous journey exploring the nuances of serverless solutions. With varied experiences from startups to research and a foundation in management and engineering, Korak brings a thoughtful and balanced perspective to the Iron.io blog.

Leave a Comment





This site uses Akismet to reduce spam. Learn how your comment data is processed.