Cron jobs are convenient tools for just about anyone who’s looking to automate recurring tasks. While they’re usually used to automate system maintenance or administration tasks, you can use them to do just about anything.
You can schedule cron jobs to run by the minute, hour, day, month, day of the week and even a combination of them all. In this article, we’re taking a look at how to run a cron job every five minutes.
Also read: How to use the Cat command in Linux?
Basic syntax for writing cron jobs
Cron jobs are made using the crontab command. Here’s the basic syntax that you should keep in mind.
* * * * * [command]
The first asterisk denotes the minutes, then come hours, day of the month, month and lastly day of the week. These fields also accept the following operators:
- *: The asterisk operator means all possible values. For example, if you put this value in the hours field, the task will be performed hourly.
- -: The hyphen operator lets you specify a range of values.
- ,: The comma operator lets you specify a list of values.
- /: The slash operator lets you specify step values that can be used along with ranges.
To write a cron job, you need to edit the crontab file. To edit this file, or create it, use the crontab -e command. For system-wide crontab files, you’re going to have to specify your username as well.
* * * * * <username> [command]
Running a cron job every five minutes
Now onto the main question, how do you run a cron job every five minutes? Well, you use the / operator in the minutes field — something like this.
*/5 * * * * [command]
Alternatively, you can also specify a range separated by five using the comma operator.
0,5,10,15,20,25,30,35,40,45,50,55 * * * * [command]
For example, if you need to run a script named python.py every five minutes, here’s what you’ll write.
*/5 * * * * python3 python.py
0,5,10,15,20,25,30,35,40,45,50,55 * * * * python3 python.py
You can play around with operators and values in the command to modify it and run the commands you want whenever you want them to run.
Also read: RM command in Linux explained with examples
Someone who writes/edits/shoots/hosts all things tech and when he’s not, streams himself racing virtual cars. You can reach out to Yadullah at [email protected], or follow him on Instagram or Twitter.