Linux Applications

How To Execute crontab Every 5 Minutes

Linux systems make it easier to automate a task. You can specify which task to perform and when a given task should be executed. The crontab makes the automation possible. A text that allows different users to schedule jobs. A scheduled job is called a cron job. This guide discusses how to schedule jobs in Linux and, in particular, how to execute crontab every 5 minutes.

How To Schedule a cron job Every 5 Minutes

Each user on a Linux system can schedule jobs independently. To schedule a cron job, you need to modify the crontab file and add the code to execute a given command at a specified time. A crontab has 5 fields separated by spaces. Each of the fields specifies the date and time a command should execute.

Here is the following syntax of a crontab file:

MIN HOUR DAY-Of-MONTH MONTH DAY-OF-WEEK

To execute a crontab every 5 minutes, we need to use the slash (/) special character followed by 5, which acts as a step that crontab should execute the command after every 5 minutes.
The command is provided below:

*/5 * * * * [command]

Note that the 5 is in the first field of Minutes. All the other fields remain with the asterisk (*), which implies using all possible values for the field.

For instance, let’s create a cron job that executes a script, crontest.sh, located in /Desktop. Start by opening a crontab file using the following command:

crontab -e

Once the file opens, we need to add the following line at the bottom of the file. Ensure there is no space between the asterisk and the slash.

*/5 * * * * /home/kyle/Desktop/crontest.sh

Note that the crontab file will open based on the editor you select to use. In this case, we are using a nano editor.

Save the file and exit. The specified job will execute every 5 minutes until you otherwise modify the cron job. Also, if the cron job has no error and gets created successfully, you should get an output similar to the one found below:

You can view the available cron jobs using the following command:

crontab -l

Also, to remove the cron job, use the following command:

crontab -r

That’s how you can execute crontab every 5 minutes.

There are also other ways to execute a cron job every 5 minutes.

1. Every 5 minutes of a specific day of the week

For instance, if you need to run a script that executes a given job every 5 minutes on a given day of the week, like Sunday, you can set your crontab file, as shown below:

*/5 * * * 0 [command]

2. Every 5 minutes of a specific hour

You can also choose to execute a job every 5 minutes on a specified hour of the day. For instance, the command would be to schedule a job to run every 5 minutes from 2:00 PM to 3:00 PM each day.

*/5 2-3 * * * [command]

3. Every 5 minutes of a given month

Also, if you need to set a job to execute every 5 minutes of a given month, all you need is to specify the month. The following command executes every 5 minutes every February:

*/5 * * 2 * [command]

4. Every 5 minutes of a specific day of the month

If you need to perform a task like creating backups every 5 minutes of a given day of the month, like every first day of each month, you can use the following command:

*/5 * 1 * * [command]

Similarly, you can create a combination using all the fields. For instance, to execute crontab every 5 minutes every Wednesday of March from 1:00 PM to 4:00 PM, the following command would be:

*/5 13-16 * 3 3 [command]

Conclusion

Executing a crontab every 5 minutes is easy, and we’ve discussed how to edit your crontab to schedule a cron job. Further, we’ve seen other examples of how to execute a job every 5 minutes on different dates and times. You can edit the command and achieve a schedule that favors your need, thanks to the foundation laid by this article. You will love using crontab.

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.