How to Create a Cron Job in Linux: Ubuntu Cron Job
Automating tasks in Linux is now more manageable, thanks to crontabs. Crontabs allow you to schedule jobs to be performed at specific times, each referred to as a cron job.
Cron jobs are shell scripts or Linux commands specified in crontab files. These files are monitored by the cron daemon, which executes the tasks at the designated times.
This article explores how to set up cron jobs in Ubuntu 22.04 and how cron jobs function in the Linux operating system.
What Is a Cron Job?
Cron is a time-based job scheduler ubiquitous in Linux and Unix-like systems. It enables you to schedule tasks to run automatically at specified time intervals, facilitating the automation of system administration and maintenance. Cron jobs on Linux are used for several purposes, including:
- Automating the process of backing up databases, files, and system directories at designated intervals. This ensures data is available in case of data loss or system failure, and reinforces data integrity.
- Linux administrators use Ubuntu cron jobs to generate and send automated reports on a schedule. These reports can provide insights into security events, system performance, and other critical activities, to enhance monitoring and decision-making.
- Periodically checking the availability of services to detect and address service interruptions.
The following sections detail how to install and use the Ubuntu cron job utility.
Prerequisites
Before setting up cron jobs on Ubuntu, ensure you have the following prerequisites:
- Ubuntu 22.04 or a stable version is running on your system or within a VirtualBox environment.
- You possess sudo or root privileges to install and manage the required utilities.
Step-By-Step Guide: How to Create a Cron Job in Linux?
The cron utility is generally preinstalled on most Linux distributions. You can verify its installation by issuing the systemctl status cron
“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 the terminal displays output similar to the above, the cron daemon is active and running. However, if the service isn’t found or the cron utility isn’t installed for any reason, use the following steps to install the cron utility on your Ubuntu 22.04 system:
Step 1: Update System Packages
The first recommended step is to update your system’s local repositories, along with all packages, via the “apt update” command:
$ sudo apt update
Step 2: Install Cron on Ubuntu
You can install the cron utility from the default Ubuntu apt repositories with the command:
$ sudo apt install cron
After installing the cron utility, you’ll be able to schedule and execute tasks periodically at defined dates, months, weeks, hours, and intervals.
Step 3: Enable and Start Cron Service
Now, enable and start the cron service so it runs in the background, even across system reboots. Issue the following commands:
$ sudo systemctl enable cron
This command creates a symbolic link on your system. Next, start the cron service:
$ sudo systemctl start cron
Verify the running status of the cron service again. Your screen will display the following output:
Create Cron Jobs on Linux
Cron jobs are defined within a configuration file called a cron table, or “crontab.” To create a new cron job, you either instantiate a new crontab or edit an existing one:
$ crontab -e
If this is the first time launching crontab on your system, you’ll be asked to select a default text editor. We’ll use /bin/nano. Select the number corresponding to your choice (e.g., 2 for nano) and press Enter.
The `crontab` file displays in the terminal, pre-populated with comments showing basic cron syntax.
Lines beginning with “#” are comments that are ignored by the cron daemon.
Cron Job Basic Syntax
A cron job follows a specific syntax, comprising two key parts: the schedule and the command to be executed.
The cron job syntax typically uses six space-separated parameters or arguments. The first five are generally asterisks representing the scheduled execution time, and the final argument is the command or shell script to run, specified with a full file path.
The five asterisks (*) in a cron schedule represent the execution time, in this order:
[minute] [hour] [day of month] [month] [day of week] [command]
Cron executes commands only when the minute, hour, and month parameters match the current time, and when at least one of the day parameters (day of the month or day of the week) matches the current date. Allowed values for each setting are: