Cron Jobs for absolute beginners
What the heck is Cron? Cron was originally existed as a preinstalled utility in old Unix systems. It was created to run scheduled jobs or in other words to execute something in a predefined time in future. Since GNU/Linux also created as a Unix like system, this functionality has been offered using multiple open source applications. Cron runs jobs repetitively, if you want to just run a command once there's another Unix utility called at which will not be explained here. Fun fact! The name Cron originated from the Greek word Chronos which means Time. Don't get confused!! There are multiple implementations of Cron for Linux. Some of the most used ones are: Cronie - Default Cron implementation on RHEL (Red Hat Enterprise Linux) and RHEL based distros. Dcron - A lightweight implementation for distro's like Alpine and embedded systems. fcron - One with support for non 24/7 systems. Vixie-cron - One of the earliest implementations. Despite these multiple applications, they all invokes using the same commands/flags and use the same file formats and share the same basic structure while some of them having additional features. SO, SIMPLY YOU DON'T NEED TO WORRY AS LONG AS YOU HAVE ONE OF THE ABOVE INSTALLED. Don't jump ahead and Install yet? Most of the GNU/Linux distro's comes with a cron application pre-installed. If it isn't, chances are it is available through their official package repositories. Ubuntu, Ubuntu based, RHEL and RHEL based distro's are usually come with cron pre-installed. For a system like Arch or Alpine linux, you need to install one from their package repositories and in that case choose one that is available and fit's for your needs. You can check if a cron application is available using the following command: sudo systemctl status cron # this will only work on distro's with Systemd init system Most modern GNU/Linux distro's use systemd but if yours not, refer distro's documentation for the init system and how to check the services status. Service is not running! Dont't worry just start it: sudo systemctl start cron For distro's with no cron (examples are for arch and RHEL based) Don't worry, just, Install it using your default package manager: sudo pacman -S cronie # for arch based sudo yum install cronie # RHEL based Enable the service: sudo systemctl enable cronie.service # for arch and RHEL based Start the service: sudo systemctl start cronie.service # for arch and RHEL based How to use There are two basic terms you need be familiar first: Cron Job - is single job (a command or a set of commands) that is scheduled to run for a particular time in future. Crontab - is a file containing one or more Cron Jobs (Crontab is a short term for Cron tables). Now, let's see how to create a cron job! First, understand the format for writing a cron job: * * * * * command_to_run │ │ │ │ │ │ │ │ │ └─ Day of the week (0–7) → 0 or 7 = Sunday │ │ │ └──── Month (1–12) │ │ └─────── Day of the month (1–31) │ └────────── Hour (0–23) └──────────── Minute (0–59) The first 5 fields are use to denote the time to the script. In an all asterisk case, the command will run infinitely for every minute. Example 1 5 * * * * echo "hi from cron job" This will run the echo command on the 5th minute of every hour infinitely. Example 2 Let's narrow it down further, 5 1 2 * * echo "hi from cron job" This job will run on the 5th minute of 1st hour of 2nd of every month infinitely. Example 3 Let's narrow further, 5 1 2 7 * echo "hi from cron job" This job will run at 1.05 AM on 2nd of July in every year inifintely. I hope you got the format now. You can run any command, any script, any program using a cron job in a Unix like systems. How to actually set this up With the format sinked in, now let's actually create a cron job. Configure your default editor: export EDITOR = nvim # I use neovim, you can put anything you want. export EDITOR = nano # if you have no idea, use this one. Execute the following command and crontab will be opened in your default editor: crontab -e NOTE: If unconfigured, it will open vi. Now write one or more cron jobs inside the opened file. 5 * * * * yay -S neofetch # I'm installing a package # Put a command that you can verify later NOTE: If you look for the location of the crontab file, you might see that it is in a temp directory. That's because once you save the file, it will move it your users default crontab. 3.Verify the status of the command manually. In my case I can see if the package has been installed. So there's no logs? Cron does print logs to system log file, but it only contains cron start, stop times and errors encountered by cron. You won't be able to capture all the details you need

What the heck is Cron?
Cron was originally existed as a preinstalled utility in old Unix systems. It was created to run scheduled jobs or in other words to execute something in a predefined time in future.
Since GNU/Linux also created as a Unix like system, this functionality has been offered using multiple open source applications.
Cron runs jobs repetitively, if you want to just run a command once there's another Unix utility called at
which will not be explained here.
Fun fact!
The name Cron originated from the Greek word Chronos
which means Time
.
Don't get confused!!
There are multiple implementations of Cron for Linux. Some of the most used ones are:
Cronie - Default Cron implementation on RHEL (Red Hat Enterprise Linux) and RHEL based distros.
Dcron - A lightweight implementation for distro's like Alpine and embedded systems.
fcron - One with support for non 24/7 systems.
Vixie-cron - One of the earliest implementations.
Despite these multiple applications, they all invokes using the same commands/flags and use the same file formats and share the same basic structure while some of them having additional features.
SO, SIMPLY YOU DON'T NEED TO WORRY AS LONG AS YOU HAVE ONE OF THE ABOVE INSTALLED.
Don't jump ahead and Install yet?
Most of the GNU/Linux distro's comes with a cron application pre-installed. If it isn't, chances are it is available through their official package repositories.
Ubuntu, Ubuntu based, RHEL and RHEL based distro's are usually come with cron pre-installed.
For a system like Arch or Alpine linux, you need to install one from their package repositories and in that case choose one that is available and fit's for your needs.
- You can check if a cron application is available using the following command:
sudo systemctl status cron
# this will only work on distro's with Systemd init system
Most modern GNU/Linux distro's use systemd but if yours not, refer distro's documentation for the init system and how to check the services status.
Service is not running!
Dont't worry just start it:
sudo systemctl start cron
For distro's with no cron (examples are for arch and RHEL based)
Don't worry, just,
- Install it using your default package manager:
sudo pacman -S cronie # for arch based
sudo yum install cronie # RHEL based
- Enable the service:
sudo systemctl enable cronie.service # for arch and RHEL based
- Start the service:
sudo systemctl start cronie.service # for arch and RHEL based
How to use
There are two basic terms you need be familiar first:
- Cron Job - is single job (a command or a set of commands) that is scheduled to run for a particular time in future.
- Crontab - is a file containing one or more Cron Jobs (Crontab is a short term for Cron tables).
Now, let's see how to create a cron job!
First, understand the format for writing a cron job:
* * * * * command_to_run
│ │ │ │ │
│ │ │ │ └─ Day of the week (0–7) → 0 or 7 = Sunday
│ │ │ └──── Month (1–12)
│ │ └─────── Day of the month (1–31)
│ └────────── Hour (0–23)
└──────────── Minute (0–59)
- The first 5 fields are use to denote the time to the script.
- In an all asterisk case, the command will run infinitely for every minute.
Example 1
5 * * * * echo "hi from cron job"
This will run the echo command on the 5th minute of every hour infinitely.
Example 2
Let's narrow it down further,
5 1 2 * * echo "hi from cron job"
This job will run on the 5th minute of 1st hour of 2nd of every month infinitely.
Example 3
Let's narrow further,
5 1 2 7 * echo "hi from cron job"
This job will run at 1.05 AM on 2nd of July in every year inifintely.
- I hope you got the format now. You can run any command, any script, any program using a cron job in a Unix like systems.
How to actually set this up
With the format sinked in, now let's actually create a cron job.
- Configure your default editor:
export EDITOR = nvim # I use neovim, you can put anything you want.
export EDITOR = nano # if you have no idea, use this one.
- Execute the following command and crontab will be opened in your default editor:
crontab -e
NOTE: If unconfigured, it will open vi
.
- Now write one or more cron jobs inside the opened file.
5 * * * * yay -S neofetch # I'm installing a package
# Put a command that you can verify later
NOTE: If you look for the location of the crontab file, you might see that it is in a temp directory. That's because once you save the file, it will move it your users default crontab.
3.Verify the status of the command manually. In my case I can see if the package has been installed.
So there's no logs?
Cron does print logs to system log file, but it only contains cron start, stop times and errors encountered by cron. You won't be able to capture all the details you need.
grep CRON /var/log/syslog # You can grep CRON logs from sys log like this
But here's a solution that I use to get a more verbose log!
mkdir logs && touch cronlogs.log # I create a logifle in my home directory
5 * * * * yay -S neofetch >> /home//logs/cronlogs.log 2>&1
# Then append the stdout and stderr of the run command to that logfile.
One more thing about Crontabs
There are some crontabs that comes built in with cron.
- User Crontab
This is the crontab we just used above to run our cronjob. When we execute
crontab -e
and write our cron job and save, it will automatically be moved to the current logged user's crontab.
Those Cron Jobs will only run for that user.
- Sytem Crontab If you're the root user or have root access, you can write to the system Crontab as well. These will allow you to specify for which user, a perticular job should run.
You open it using your preffered editor:
sudo nvim /etc/crontab
The format is the same with the addition of the username:
minute hour day month weekday **user** command
NOTE: One Cron Job only can have one user
Don't leave without reading this
Cron used to be the defacto for scheduling tasks for DevOps, but it does have many problems. Some notable ones are,
- No proper error handling to retry on failuires.
- Seperate verbose logs needs manual setup.
- No job dependancy support.
- Cannot handle missed jobs. (Though some implementaions can)
Systemd Timers are the future
This is the modern take on Cron Jobs without it's drawbacks and now widely used as a replacement to it.
it is a bit complex to setup but have more control over the jobs and have a better intergration with the system.
I will explain how to use Systemd Timers to take full advantage of it in my next article but if you're curious and can depend on official docs, go ahead and do your own reading as well.
Systemd timers man page -> https://www.freedesktop.org/software/systemd/man/latest/systemd.timer.html