Setting Up Cron Jobs Linux

Cron jobs Linux are an easy way to automate tasks in Linux. Sometimes setting up cron jobs in Linux can be tricky, but with this step-by-step guide, it doesn’t have to be! We’ll walk you through all the steps from creating the job to scheduling it and monitoring its progress.

What is a Cron Job?

A cron job is a scheduled task that is set to run at a specified time. It runs in the background to automate tasks that would otherwise require manual execution such as running scripts or commands. Once set up, cron jobs require minimal maintenance and can free up your time for more important activities.

Creating a Cron Job File

Now that the cron service is running on your system, you can create a cron job file. These files specify when to run a script or command, and are stored in different locations depending on the type of Linux distribution you are running. For example, on Ubuntu/Debian systems, cron job files are stored in /etc/cron.* directories.

But the good thing about creating cron job is that the tool provides a command crontab -e. The command opens up the cron file without you typing the cron file path on the system.

Setting the Cron Job Schedule

Once you’ve identified the location of your cron job file, you can start to set up the schedule for your command. Cron jobs are run on a specific schedule based on the crontab syntax. This syntax is composed of five components that indicate when a command should be executed: minute (m), hour (h), day of month (dom), month (mon), and day of week (dow). By specifying values for each of these components, you can tell cron when to run a particular command.

How to Access the Cron Job Feature in Linux

To access the cron job feature in Linux, you’ll need to use a terminal shell such as Bash. You can open a shell by pressing the “CTRL+ALT+T” keyboard shortcut. Once the shell is open, type crontab –e and press enter. This will open the vi editor within which you can modify your cron jobs file.

Creating a Linux Cron Job

The crontab file is a configuration file used by the cron daemon to schedule jobs. It contains a list of commands and their execution schedules.

To edit the crontab file, the user needs to open it using a text editor and add a new line for the job, specifying the time and command to be executed.

crontab -e
open crontab file

Linux uses cron jobs to schedule tasks for specific times and intervals. To create a new cron job, open the cron job editor and type in the command you want to run. For basic functions, such as printing a phrase or running a script, you’ll usually need to add additional flags or parameters. Once you’ve set up your cron job, it will automatically trigger based on the time and interval you specified.

* * * * * /path/to/command

The five stars or asterisks represent minute, hour, day of the month, month and day of the week. Below you will see more examples to help you learn more about the use.

List all cron jobs

crontab -l

One-time cron jobs

One-time cron job runs at a specific date and time. It does not repeat hence the name one-time cron job. For instance, the following command will run at 2:30 PM on March 25th, 2023 –

30 14 25 03 * /path/to/command

Recurring cron jobs Linux

Recurring cron job runs repeatedly at a specific time. For example, we can run a command every minute, hour, week or even specific day of the week. The following command will run every Monday at 2:30 PM –

30 14 * * 1 /path/to/command
# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of the month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of the week (0 - 7) (Sunday is 0 or 7)
# │ │ │ │ │
# │ │ │ │ │
# * * * * *  command to execute
  ─ ─ ─ ─ ─
  │ │ │ │ └─── Day of the week (0-7, where both 0 and 7 mean Sunday)
  │ │ │ └──────── Month (1-12)
  │ │ └───────────── Day of the month (1-31)
  │ └──────────────── Hour (0-23)
  └───────────────────── Minute (0-59)

Scheduling cron jobs with special characters

It is common to include special characters in the cron job entries. Since special characters such as * (asterisk) and ” (double quotes) have special meaning in the cron syntax, we can use backslash to escape the * in the command.

0 18 * * 1-5 /path/to/command arg\*

In the above command, the system will consider the last * (asterisk) as the part of the command instead of the command syntax.

Run command every minute

* * * * * /path/to/command

Run command every hour

0 * * * * /path/to/command

Run command every day

The following command will run at 1:00 PM everyday.

0 13 * * * /path/to/command

Scheduling a cron job every week

The following command will at 3:30 PM every Friday.

30 15 * * 5 /path/to/command

Scheduling a cron job every month

The following command will run at 12:30 AM every 1st day of the Month –

30 0 1 * * /path/to/command

Verifying cron jobs

/var/log/syslog or /var/log/cron files often include information about cron jobs in the system logs. To find specific records related to your cron job, use the grep tool. For instance, you can use the following command to look for records associated with the cron job that executes the command /path/to/command once every hour.

grep "/path/to/command" /var/log/syslog

Use tail to read the system log file.

tail -f /var/log/syslog

The above grep command will return the log of the command /path/to/command if the command ran successfully.

Most common use cases for cron jobs

  • Automating backups
  • Automating system updates
  • Automating reports
  • Automating database maintenance
  • Automating email notifications

Conclusion

Cron job is one of the most important tool for Linux server administrators or even for regular desktop users who need automate imoprtant tasks. We can combine it with other tools such as rsync or scp to create server backup and store it on a remote computer.

If you do no use cron jobs, I highly recommend to learn it. If you need any help with any section of this article, please let me know in comment section below.