How to Create a Cron Job in Linux: Ubuntu Cron Job
Automating tasks in Linux has now become more manageable. Namely, this feature is achieved through crontabs, which allows us to schedule multiple jobs that users want to perform at a specific time, each referred to as a cron job.
The cron jobs are shell scripts or Linux commands specified in the crontab files that are further monitored by the cron daemon and tasks executed at a particular time.
In this article, we will explore how to set up cron jobs in Ubuntu 22.04 and how cron jobs work in the Linux operating system.
What Is a Cron Job?
Cron is a time-based job scheduler for Linux/Unix distributions. It allows you to schedule jobs to run after a specific time interval. You can automate system administration and maintenance tasks in the Linux operating system using cron jobs. You can also use cron jobs on Linux for various purposes, such as:
- Automate the process of backing up databases, files, and system directories at specified time intervals. This not only ensures data availability in case of system failure or data loss but also focuses on data integrity.
- Linux Administrators can use Ubuntu cron jobs to generate and send automated reports at a scheduled time. These reports provide insights into security events, system performance, or other critical activities. In this way, cron jobs help enhance monitoring and decision-making processes.
- Using crontab, you can periodically check the availability of services. This monitoring approach helps in detecting and addressing service interruptions.
Below, we will see how to install and use the Ubuntu cron job utility.
Prerequisites
To install and use the cron job on Ubuntu, you should have the following prerequisites:
- The Ubuntu 22.04 or stable version should be running on your system or inside a VirtualBox.
- You should have sudo or root privileges to install and run the required utilities.
Step-By-Step Guide: How to Create a Cron Job in Linux?
The cron utility comes preinstalled on most Linux distributions. However, you can verify the installation using the “systemctl status cron” command. Open the terminal using “Ctrl + Alt + t” and type the command below to check the running status:
$ sudo systemctl status cron
Installing Cron Utility on Ubuntu 22.04
If you receive the same result on your terminal, the cron daemon is up and running on your Ubuntu system. But, if service is not found or cron utility is not installed on your system due to any reason, you can install cron utility on your Ubuntu 22.04 system using the following steps:
Step 1: Update System Packages
First, the recommended step is to update your system’s local repositories, including all packages, using the “apt update” command:
$ sudo apt update
Step 2: Install Cron on Ubuntu
To use cron jobs, you need to install a cron utility on your Linux system. In Ubuntu 22.04, you can install the cron utility from the default Ubuntu apt repositories by using the following command:
$ sudo apt install cron
Once you install the cron utility on your system, you can schedule and execute tasks periodically at specific dates, months, weeks, hours, and time intervals.
Step 3: Enable and Start Cron Service
Now, enable and start the cron service to run this service in the background, even if you reboot your system. Use the following command to do it:
$ sudo systemctl enable cron
After enabling this service, a symbolic link will be created on your system. Start the cron service by using this command:
$ sudo systemctl start cron
Now, verify the running status of the cron again. The following output should be displayed on the screen:
Create Cron Jobs on Linux
All cron jobs are specified inside a configuration file cron Table known as “crontab.” So, to create a new cron job, you need to instantiate or start the crontab using the below command:
$ crontab -e
If you start the crontab for the first time on your system, you will need to choose an option to select a file editor that you can use for running your cron jobs. Here, we will select a /bin/nano editor and press Enter.
The crontab file will display on the terminal. This file will populate the terminal with the basic syntax of cron expressions.
Here, you will notice the “#” symbol at the beginning of each line. This symbol signifies a line comment.
Cron Job Basic Syntax
A cron job is written in a specific cron expression. This expression consists of two parts: the scheduled time and the command that you would like to execute in your terminal.
Usually, the cron job syntax has six parameters or arguments separated by spaces. So, the first five are asterisks representing the scheduled execution time, and the other argument is a command or shell script with a complete file path.
When you schedule a cron job on your system, it uses the five asterisks (*) pattern. Each star represents the time based on the below order:
[minute] [hour] [day of month] [month] [day of week] [command]
Cron executes commands only when the minute, hour, and month settings match with the current time, and at least one of the day configurations, whether it's the day of the month or the day of the week, matches the current date. You can use specific allowed values for these settings:
Let’s explain the cron execution time syntax with the help of an example:
Example
For example, you want to run a cron job at 2 p.m. every Wednesday. In this case, your cron job syntax will be:
0 12 * * 3 <command>
In the above cron expression, “0” means the cron job will run once, “12” means the cron job will run at 12 p.m. (using 24-hour format), the “* *” means the job will run every day for every month, and the “3” represents that it will run on Wednesday.
Let’s take another example: you want to display a “message string” with a date every minute using the echo command. This string will further append to the “testfile.text” file.
* * * * * echo "Welcome, $date" >> $HOME/Documents/testfile.txt
The above command will create a file in your system’s home/Documents directory with the name “testfile.txt.”
Open this file, and you will see the following content:
Managing Cron Jobs on Linux
Once you create a cron job, you can easily manage the cron jobs. It's important to exercise caution when working with crontab because incorrect settings or configurations can result in unexpected outcomes. It's also important to regularly monitor and test scheduled tasks to guarantee that your automated processes run correctly.
To list all cron jobs, you can display them using the following command:
$ crontab -l
As you can see in the screenshot, only one cron job is executed, and it’s the one created above.
List User-Owned Cron Jobs on Linux
Similarly, you can also manage user-owned cron jobs. The crontab files for each user are stored in the spool area. If you list all the spool directory files, you can easily keep track of user-owned crontab files using the following command:
$ ls /var/spool/cron/crontabs
You can also list user-owned crontab files by using the following command with “root” privileges:
$ crontab -u <user-name> -l
Replace “username” with your username.
Delete Ubuntu Cron Job
If you want to delete a cron job on Linux, use the following command:
$ crontab -r
To delete a cron job of a specific user, use this command:
$ crontab -r -u <username>
Edit a Cron Job
If you want to edit a file for a specific user, you can edit the cron job using the following command:
$ crontab -u <username> -e
List Cron Jobs Log
By default, the activities and logs related to the cron service are stored in the global logging file located at "/var/log/syslog." To view the cron service's activity and messages specifically, you can use the "grep" command to filter and extract the relevant information from the syslog file.
$ grep CRON /var/log/syslog
This allows you to isolate and display only the entries related to cron service activities.
You can also manage the system-wide crontab files. These system crontabs, which contain scheduled tasks for the entire system, are restricted to the root user. They cannot be modified by any other user.
Unlike user-specific crontabs, there is no dedicated "crontab" command available to directly edit these system crontab files. Instead, system administrators must access and edit these files directly to make any changes or additions to the system-wide scheduled tasks. To access the content of these system-wide files, use the below-provided command:
$ nano /etc/crontab
If you are using VPS hosting for your Linux system, use the above steps to add new cron jobs using crontabs. Similarly, by using different tools, you can manage cron jobs easily on your machine.
Conclusion
In this guide, we learned how to use the cron utility on Ubuntu 22.04 to automate tasks on Linux. Cron is a utility that helps you plan and set up tasks to run at specific times, on particular days, or at regular intervals.
Cron is a handy tool often used by system administrators to make their work easier. After going through this guide, you should now have a good understanding of how to use cron to schedule tasks on your Linux systems.
If you need more information, you can always check out the official Cron documentation page or visit the crontab manual page. For crontab's help, use the “man cron” command through the terminal.