How to Run Cron Jobs Every 5, 10, or 15 Minutes on Linux

Cron is a time-based job scheduler in Linux operating systems that allows users to schedule commands or scripts to run at specified intervals. It is a powerful tool that helps automate repetitive tasks and is essential for system administration. In this article, we will focus on how to set up cron jobs to run every 5, 10, or 15 minutes on Linux.

Understanding Cron

Before we get into the specifics of setting up a cron job, it is essential to understand how cron works. Cron runs in the background, continuously checking for scheduled tasks. It uses a configuration file called crontab to read the tasks and their schedules. Each user has a unique crontab file containing their scheduled tasks.

Establishing a Basic Cron Job in Linux Systems

Cron jobs are managed by the cron daemon, a background process that operates on all Linux systems. To establish a basic cron job, it is necessary to generate a new crontab file, which consists of a series of commands and their corresponding execution times. The command below can be used to create a new crontab file:

crontab -e

This command opens a text editor in which the cron jobs can be entered. Within the crontab file, each line signifies an individual cron job and adheres to the following structure:

minute hour day month weekday command

The components of this structure are as follows:

  • Minute: Specifies the minute at which the command is to be executed, with values ranging from 0 to 59.
  • Hour: Indicates the hour at which the command is to be executed, with values ranging from 0 to 23.
  • Day: Denotes the day of the month on which the command is to be executed, with values ranging from 1 to 31.
  • Month: Represents the month in which the command is to be executed, with values ranging from 1 to 12.
  • Weekday: Signifies the day of the week on which the command is to be executed, with values ranging from 0 to 7, where both 0 and 7 correspond to Sunday.
  • Command: Refers to the command that is to be executed.

For instance, to schedule a command to run every day at 5 pm, the following line should be inserted into the crontab file:

0 17 * * * command

Scheduling Cron Jobs to Run Every 5, 10, or 15 Minutes

To configure a cron job to execute every 5, 10, or 15 minutes, it is necessary to adjust the minute field in the crontab file. The minute field, which is the first entry in the line, denotes the minute at which the command should be executed.

To schedule a cron job every 5 minutes, the following line should be input into the crontab file:

*/5 * * * * [command to run]

The first field indicates the minute, with “*/5” signifying that the job will run every 5 minutes. The remaining fields, represented by asterisks, convey that the job can be executed at any hour, day, month, or day of the week.

The example below demonstrates how to execute a script file every 5 minutes:

*/5 * * * * /path/to/script.sh

To schedule a cron job every 10 minutes, the following line should be input into the crontab file:

*/10 * * * * [command to run]

The first field indicates the minute, with “*/10” signifying that the job will run every 10 minutes. The remaining fields, represented by asterisks, convey that the job can be executed at any hour, day, month, or day of the week.

The example below demonstrates how to execute a script file every 10 minutes:

*/10 * * * * /path/to/script.sh

To schedule a cron job every 15 minutes, the following line should be input into the crontab file:

*/15 * * * * [command to run]

The first field indicates the minute, with “*/15” signifying that the job will run every 15 minutes. The remaining fields, represented by asterisks, convey that the job can be executed at any hour, day, month, or day of the week.

The example below demonstrates how to execute a script file every 15 minutes:

*/15 * * * * /path/to/script.sh

Tips for Setting Up Cron Jobs in Linux

Here are a few tips for setting up cron jobs in Linux:

  • Test your cron jobs before adding them to the crontab file. You can use the following command to test the cron job.
crontab -l | grep command

This will run the command and display the output, allowing you to see if it works as expected before adding it to the crontab file.

  • Be careful when specifying the time fields. If you specify the wrong time, the cron job may not run as expected. It’s always a good idea to double-check the time fields before saving the crontab file.
  • Use full paths for commands in the crontab file. This ensures that the correct version of the command will be executed, even if there are multiple versions of the command installed on the system.
  • Keep the crontab file organized. If you have multiple cron jobs, it’s a good idea to keep them organized by adding comments to the crontab file to describe what each cron job does.

Crontab Mail: Grasping the Significance of Email Notifications

When executing a cron job, the system generates an email automatically to inform the user about the job’s status. Receiving such notifications can be particularly useful for monitoring tasks that require attention, such as backup processes. The email provides information on whether the job was successful or encountered errors.

However, managing multiple cron jobs can lead to a cluttered inbox due to the numerous emails generated. To prevent this, it is possible to modify or disable email notifications originating from cron jobs.

One method to modify email notifications involves redirecting the cron job’s output to the null device, /dev/null. This unique file discards all data written to it, effectively suppressing the cron job’s output.

The example below demonstrates how to redirect a cron job’s output to /dev/null:

# Edit the crontab file
crontab -e

# Add the following line to the file to run a script every 10 minutes, but redirect the output to /dev/null
*/10 * * * * /path/to/script.sh > /dev/null 2>&1

In this example, the > /dev/null operator redirects the script’s standard output to /dev/null, while the 2>&1 operator redirects the standard error output to the same location, effectively suppressing all output.

Alternatively, the MAILTO variable in the crontab file can be adjusted to specify a different email address or completely disable email notifications. The example below shows how to disable email notifications:

# Edit the crontab file
crontab -e

# Add the following line to the file to disable email notifications
MAILTO=""

In this example, assigning an empty string to the MAILTO variable effectively disables email notifications.

Conclusion

In conclusion, running cron jobs every 5, 10, or 15 minutes on Linux systems is a simple process once you understand the format of the crontab file. By following the steps outlined in this guide and using the tips provided, you can set up cron jobs to run at specific intervals and streamline your Linux systems. Don’t forget to test your cron jobs before adding them to the crontab file, and keep the crontab file organized to ensure that your cron jobs run smoothly.

Your Mastodon Instance
Share to...