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:
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:
m | h | dom | mon | dow |
Minute | Hour | Day of Month | Month | Day of Week |
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 update
Code 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 update
Code language: JavaScript (javascript)
Hit CTRL + O to write the file and CTRL + X to leave (Nano)
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.
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 ?
Of course you are absolutely right π I corrected it! Thank you!