Published on

Introduction to Cron Jobs

Viewed

times

Authors
  • Name
    Twitter
Series: Automating Periodic Website Pinging with Cron Jobs in a Conda Environment

Welcome to the first part of our series on Automating Periodic Website Pinging. In this post, we’ll learn about cron jobs—an essential tool for automating repetitive tasks on Unix-based systems. Whether you're a developer, system administrator, or just looking to streamline your workflows, learning about cron jobs will be incredibly useful.

Prerequisites

Before we dive in, make sure you have:

  • Basic command line familiarity
  • Access to a Unix or Unix-like operating system (such as Linux or macOS)

What are Cron Jobs?

A cron job is essentially a scheduled task that runs automatically at specified intervals . These tasks could be something like taking perodic backups, checking system updates, or running monitoring scripts.

Setting Up Cron Jobs

Setting up cron jobs is a straightforward process. Don't worry if you're not familiar with the syntax yet – we'll cover that in detail later.

Accessing the Crontab

The crontab file is where you define your cron jobs. In your terminal, open the crontab file for editing by entering the following command:

crontab -e

If it's your first time, you might be prompted to choose a text editor (like vi/vim or nano). Select your preferred option. If you're unsure, select nano.

Add a CronJob

In the opened file, each line represents a separate cron job. Let's add a simple example to print the text 'Hello World !'.

* * * * * echo "Hello World !" >> ~/cron_log.txt

This will add the message "Hello World !" to a file named cron_log.txt in our home directory every minute.

Save and Exit

Let us save the file and exit the editor. If you picked nano as your editor:

  • Press Ctrl + X
  • Press Y to confirm
  • Press Enter to save

For Vi/Vim:

  • Press ESC
  • Type :wq and press Enter

Congratulations! You've just set up your first cron job.

Verify Cron Jobs

To verify the cron jobs we have set up, use command:

crontab -l

This will list all the cron jobs for the current user. Now that we setup our first cronjob, let us understand cron syntax more in detail.

Understanding Cron Syntax

Cron job syntax consists of five time fields followed by the command we want to execute. Here’s what it looks like:

* * * * * /path/to/command

Each of these * symbol represent a specific time unit.

FieldValueDescription
Minute*Every minute (0-59)
Hour*Every hour (0-23)
Day of Month*Every day of the month (1-31)
Month*Every month (1-12)
Day of Week*Every day of the week (0-7, where both 0 and 7 represent Sunday)

so, our earlier command * * * * * echo "Hello World !" >> ~/cron_log.txt runs every minute of every hour of every day of the month i.e it runs every minute .

Here are a few other examples with different scheduling options:

TimingCommandDescription
0 12 * * */path/to/commandExecutes the command every day at 12:00 PM (noon).
30 8 * * 1/path/to/commandExecutes the command every Monday at 8:30 AM.
0 0 1 * */path/to/commandExecutes the command on the first day of every month at midnight.
0 0 1 1 */path/to/commandExecutes the command on January 1st at midnight.
*/5 * * * */path/to/commandExecutes the command every 5 minutes.
Advanced Cron Techniques

While we've covered the basics, cron offers more advanced features:

  • Ranges: Use hyphens to specify ranges. For example, 1-5 in the day-of-week field means Monday to Friday.
  • Lists: Use commas to specify lists. For example, 1,3,5 in the day-of-week field means Monday, Wednesday, and Friday.
  • Step values: Use */n to specify steps. For example, */10 in the minute field means every 10 minutes.

In the next post of the series, we will go through setting up a cronjob with a anaconda environment.

Test Your Knowledge
What does the cron schedule 0 6 * * * do?

Executes the command every day at 6:00 AM

How would you schedule a cron job to run at 3:15 PM every Friday?

15 15 * * 5 Executes the command at 3:15 PM every Friday