Cronjob – The Complete Guide to Cronjobs

In this tutorial, you will learn everything you need to know about Cronjob. We will cover what a cronjob is, and how to create one and we will try to make it less confusing.

Table of Contents

🐧 There are some related articles!
👉 Top Things to do after installing Kali Linux
👉 Linux on Windows – The Complete WSL 2 Tutorial Series
👉 Create Generalized Windows Images with FOG Server
👉 Install FOG Server on Ubuntu
👉 Install Kali Linux on Windows – Complete Guide
👉 The Complete Kali Linux and Windows Dual Booting Guide
👉 Cronjob – The Complete Guide to Cronjobs
👉 Open Bitlocker Drive on Linux – Quick & Easy

What is a Cronjob?

A Cronjob is a task that gets done automatically at a specific time. Let’s take sudo apt-get update for example.

If you want to keep your apt repository up to date at all times, you can utilize a Cronjob to do that for you, once a day for example.

How does a Cronjob work?

We use an instance of Ubuntu for this example. After you are logged in, run:

sudo crontab -e

If you run this for the first time, you will get asked which editor you want to use per default. We recommend using Nano for beginners.

And this is how your crontab file looks like when you first open it:

Cronjob explained

This crontab file is the place where you want to add all of your cronjobs in. It is important to familiarize yourself with the syntax.

Now, when you look at the bottom line, you can see m h dom mon dow command

This is what you need to understand, so let’s break it down:

mhdommondow
MinuteHourDay of MonthMonthDay of Week
Cronjob Explained

Alright, this should shed some light already.

Now we need to understand with which values we can work:

  • Minutes: 0 – 59
  • Hours: 0 – 23
  • DOM: 1 – 31
  • Month: 1 – 12
  • DOW: 0 is Sunday and 7 is Sunday for example:
    • 0 – 6 => Sunday – Saturday or
    • 1 – 7 => Monday – Sunday
    • You usually dont use 7 tho, only 0-6
  • * Is a Wildcard, which means basically if you take the minute field and put a wildcard in it, it will run every  Minute.

Let’s do a little test for yourself: Set a Cronjob that runs every Saturday at 14:30.

Answer:

30 14 * * 6

Alright, so this should give you a pretty good idea of how to set the time accordingly. The whole command would then look something like this:

15 23 15 * * sudo apt-get updateCode language: JavaScript (javascript)

This would run sudo apt-get update at 23:15, every month on the 15th, no matter which day.

In case you really can’t wrap your head around it, head over to https://crontab.guru/ and let this tool do the thinking for you.

Setting up a Cronjob

And finally let’s actually set the Cronjob:

sudo crontab -e

Scroll down all the way to the bottom of the file and enter your desired time and command:

30 14 * * * sudo apt-get updateCode language: JavaScript (javascript)

Hit CTRL + O to write the file and CTRL + X to leave (Nano)

Cronjob explained

Conclusion

This should give you a pretty good idea of how Cronjobs actually work. Once you have wrapped your head around the logic, it’s very simple.

2 thoughts on “Cronjob – The Complete Guide to Cronjobs”

  1. You ask a question „Set a Cronjob that runs every Saturday at 14:30.“ and said
    answer is 30 14 * * 0. Shouldnt be 30 14 * * 6 ?

    Reply

Leave a Comment

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

Share via
Copy link
Powered by Social Snap