How to Set Cron to Run Every 5 Minutes in Linux Systems

How to Set Cron to Run Every 5 Minutes in Linux Systems

Automating tasks in Linux has never been easier. With the crontab utility, users can define when specific tasks, known as cron jobs, should be performed. In this guide, we focus on setting a cron job for every 5 minutes.

Cron Expression for Every 5 Minutes

Every user in Linux can schedule cron jobs independently. To set a cron every 5 minutes, you'll need to modify the crontab file. The crontab comprises five fields separated by spaces, each determining when a command will be executed.

The standard syntax is:

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

For a cron expression to run every 5 minutes, use the slash (/) special character followed by 5, signifying the interval at which the crontab executes the command.

Here's the command:

*/5 * * * * [command]

In this expression, */5 in the MIN field ensures the command runs every 5 minutes. The asterisk (*) in the other fields denotes using all possible values.

For instance, to run a script named crontest.sh on the Desktop every 5 minutes:

  1. Open the crontab with:
crontab -e
  1. Add the line:
*/5 * * * * /home/user/Desktop/crontest.sh

The editor type (e.g., nano) used to open the crontab file depends on your default settings.

Once saved, the script will be executed every 5 minutes. To view active cron jobs, use:

crontab -l

If you wish to delete the cron job:

crontab -r

What Does Cronjob /5 * * * * Mean?

The expression */5 * * * * means the associated command will run every 5 minutes.

Advanced Cron Job Setups for Every 5 Minutes

  1. Every 5 minutes on Sundays:
*/5 * * * 0 [command]
  1. Every 5 minutes from 2 PM to 3 PM:
*/5 2-3 * * * [command]
  1. Every 5 minutes in February:
*/5 * * 2 * [command]
  1. Every 5 minutes on the first day of every month:
*/5 * 1 * * [command]

For more complex timings, like every 5 minutes every Wednesday in March from 1 PM to 4 PM:

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

Conclusion

Setting a cron job for every 5 minutes is straightforward. With the examples provided, you can customize your crontab schedule to suit specific needs. Embrace the power of the crontab in Linux and optimize your tasks seamlessly.

Read more