Cron job scheduling with Crontab job

We need to configure automatically updating our system using Cronjob to ensure that our system is always up to date. In addition to missing out on important updates, manually updating could lead to your device being vulnerable to security risks and software bugs. Automating tasks at regular intervals by using cronjobs makes your life easier.

Using Ubuntu, I created this tutorial. After you install cron on your system, you can then follow this same process to make it work on any Linux distro. Only the installation process will differ for the tutorial.

Installing Crontab job on Your System

cron is a Linux tool for scheduling and running commands and scripts at a specified time and date. Typically, scripts or commands that execute in the background are called cron jobs. Despite the fact that most Linux distributions come with cron by default, it is necessary to install it on an Ubuntu system if it is not already installed. 

In order to do this, we must update our default repository using the apt package management service. You will need to open the terminal on your Ubuntu system and type the following commands. 

sudo apt update && sudo apt upgrade

Once the repository is available, we can proceed with the tutorial. In order to install the cron utility on Ubuntu, we will use the apt package management system. Here is how to do it.

sudo apt install cron

It is possible that you will be asked for your password. Installing this utility on your system will install it.

Now, we will enable the cron service to run in the background on our system using the systemctl command. This is how the command should look.

sudo systemctl enable cron

Now that the cron has been installed, we can start scheduling jobs with the utility.

Where’s the Crontab Configuration File?

The crontab file holds the schedules of the jobs generated by the cron utility. Local crontabs for each user are stored in /var/spool/cron/crontabs/, while a system’s crontabs are located in /etc/crontab. The setup of our automatic updates will be done with this file.

Note that we can run cron with sudo privileges from our user’s local crontab. Although, sudo user passwords are required during every session for security reasons. Therefore, we bypass the security check by using the root user.

crontab -eEdit crontab file, or create one if it doesn’t already exist.
crontab -lDisplay crontab file contents.
crontab -rRemove your current crontab file.
crontab -iRemove your current crontab file with a prompt before removal.
crontab -u <username> - Edit other user crontab file. This option requires system administrator privileges.

Understanding the Crontab file format

Jobs stored in the global crontab are stored in the following format.

[minute] [hour] [day_of_month] [month] [day_of_week] [user] [command_to_run]

The fields in the syntax are listed in the following table along with the allowed values for each field.

FieldValues allowed
Minute0 to 59
Hour0 to 23
Day of month1 to 31
Month1 to 12 OR JAN to DEC
Day of week0 to 6 OR SUN to SAT

Apart from this, you can use a special character as * (asterisk). This is used to denote ‘each’. For example, if you wish to run a command each Wednesday at 5 PM, you use the following entry.

0 17 * * 3 [command]

This will run [command] on the fourth day of the week (Wednesday, we use 3 because computers count from a 0), every month, and every day of a month (because these are denoted by an asterisk, at 18 hours and 0 minutes (7:00 PM). Some newer cron daemons may allow for a special syntax. These special entries along with what they represent are given in the table below.

Special SyntaxRegular equivalent
@hourly0 * * * *
@daily       0 0 * * *
@weekly0 0 * * 0
@monthly0 0 1 * *
@yearly0 0 1 1 *

Now that we understand how the cron utility works, let us set up our automatic updates.

Setting up custom Cronjob scheduling using Crontab

In this example, we will schedule the cronjob of updating our system to the global crontab. To do so, we open the file using our preferred text editor.

sudo nano /etc/crontab

Once we enter the file, we will add the following command to the cron jobs list. This will schedule the system to call apt update and apt upgrade through the root user every Wednesday at 8 PM. These details will be saved in a file named automaticupdates.log for us to check later.

50 19 * * 3 root /usr/bin/apt update -q -y >> /var/log/apt/automaticupdates.log
0 20 * * 3 root /usr/bin/apt upgrade -q -y >> /var/log/apt/automaticupdates.log

After making the changes, we save the file and exit. Now we have successfully set up automatic updates on our Ubuntu system. These updates will happen every Wednesday, at 7:50 pm and 8:00 pm.

Final Thought

This Linux application is used to schedule and manage jobs for your system. In order to automate repetitive tasks and perform maintenance tasks on your system, they are widely used. Cronjobs was used to configure automatic updates in Ubuntu in this tutorial. We would love to hear your feedback, questions, or suggestions in the comments section below.

Leave a Comment