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:
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:
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:
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.
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:
Also, to remove the cron job, use the following command:
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:
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.
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:
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:
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:
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.