What is a Cron Job and how to write to the Crontab

By Dillon Smart · · · 0 Comments

The need to schedule tasks on a machine is common for any Software Developer, System Admin or IT professional, so it’s important to learn what a cron job is and how to write to the crontab.

What is CRON?

Cron is a daemon built-in to most Unix-like operating systems such as:

  • Unix
  • GNU/Linux
  • MacOS

Cron reads the crontab (cron tables) for predefined commands and scripts, named Cron Jobs.

By using a specific syntax, we can configure a cron job to schedule scripts or other commands to run automatically.

Cron Syntax

To be able to set up a cron job, we need to understand the basic elements that make up the syntax.

The basic syntax of a crontab line is as follows:

* * * * * python3 /path/to/file/my-script.py

This crontab entry would execute the python script my-script.py every minute.

Let’s break this crontab line apart to understand its elements.

Cron Job Time and Date

The first part of the line `* * * * * *` conists of the time/date to run the command.

In this case we have used an asterisk in each part of the definition, which means the cron will fire on each change of the value.

Going from left to right, where the minute definition is the first asterick, the time and date for cron jobs is as follows:

  • Minute (0, 59)
  • Hour (0, 23)
  • Day (1, 31)
  • Month (1, 12)
  • Weekday (0, 6)

By using an asterick for each element in the time/date definition, our cron will fire every minute.

From here on, I will refer to the definitions left to right, so the minute definition would be the second asterick.

Fire a cron job every hour

If we wanted our cron job to run every hour, we would use:

0 * * * * python3 /path/to/file/my-script.py

In plain English, this means on every hour, day, month and day of week change, at minute 0 fire the command.

If we wanted the cron job to be executed every hour on minute 30, we would change it to this:

30 * * * * python3 /path/to/file/my-script.py

Fire a cron job every day

To run a cron job every day, on the first minute of the first hour, we would write:

0 0 * * * python3 /path/to/file/my-script.py

To run a cron job every day at 01:30, we would write:

30 1 * * * python3 /path/to/file/my-script.py

Fire a cron job every Sunday at 11pm

To run a cron job on a specific day of the week at a specific time we would write:

0 23 * * 6 python3 /path/to/file/my-script.py

Check out Crontab.guru to play around and configure the correct time and date to schedule your cron jobs.

How to run a Cron Job every second?

Unfortunetly, cron does not support intervals of less than 1 minute. However, there are ways to overcome this limitation by ustilising sleep functions of programming languages.

For example, if we wanted a cron job to run every 30 seconds, we could create two identical crontab entries, where the only difference is one of them first “sleeps” for 30 seconds.

Here is an example of a shell script being executed by cron every 30 seconds.

* * * * * /path/to/script.sh
* * * * * (sleep 30; /path/to/script.sh)

Technically, cron executes both jobs at the exact same time, however, in the second crontab line the script is halted for 30 seconds.

Conclusion

Cron jobs are a fundemental way of scheduling tasks to run at specific dates and times. In this post we have learned how to schedule a task to run every minute, hour and day, as well as learning how to overcome the limitations of Crontab by running a cron job every 30 seconds.

LinuxMacOSUbuntu

0 Comment

Was this helpful? Leave a comment!

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

The ULTIMATE guide to setup Windows Subsystem for Linux (WSL) with Ubuntu, Apache2, MySQL and PHP

Updated 16th August 2022

Windows is now good for development! Yes! All thanks to Windows Subsystem for Linux (WSL). No more WAMP or Xampp, WSL can do everything you need. In this ultimate guide to setup Windows Subsystem for Linux with Ubuntu, Apache2, MySQL, and PHP we will cover: What is Windows Subsystem for Linux? Setting up Windows Subsystem

IPtables – How to block and unblock IP Addresses in Linux

Updated 14th December 2023

Not long ago, I noticed some strange activity on a server I manage. The server, running Ubuntu 22.04, was receiving high volumes of traffic from a single IP Address. After some investigation, I decided the activity resembled that of web scrapping, so I decided to block the IP Address. Iptables or ufw I hear you

Start WSL2 services on Windows startup

Updated 16th August 2022

In some circumstances, you may want to automatically start your WSL2 (Windows Subsystem for Linux) services on Windows startup such as Apache2, Nginx, and MySQL. Follow along below to learn how to start your services when you boot your machine. Create a startup script The first step is to create a startup script. This is