Understanding Crontab: 10 Practical Examples

Cron is a time-based job scheduler in Unix-like operating systems. Users can schedule jobs (commands or scripts) to run at specific times or on specific days. Here are 10 practical examples of crontab commands.

1. Run a command every minute

* * * * * /path/to/command

This command will run every minute of every hour of every day of every month. The five asterisks represent minutes (0 – 59), hours (0 – 23), days (1 – 31), months (1 – 12), and days of the week (0 – 7 where both 0 and 7 are Sunday), respectively.

2. Run a command every 5 minutes

*/5 * * * * /path/to/command

This command will run every 5 minutes. The */5 in the minutes field is equivalent to saying “every 5th minute”.

3. Run a command at 2:30 PM on weekdays

30 14 * * 1-5 /path/to/command

This command will run at 2:30 PM from Monday to Friday. The 1-5 in the days of the week field is equivalent to saying “every day from Monday through Friday”.

4. Run a command on the first day of every month

0 0 1 * * /path/to/command

This command will run at midnight (00:00) on the first day of every month. The 1 in the days field is equivalent to saying “the first day of the month”.

5. Run a command every 15 minutes during the 6PM to 6AM period

*/15 18-23,0-6 * * * /path/to/command

This command will run every 15 minutes from 6PM to 6AM. The 18-23,0-6 in the hours field is equivalent to saying “every hour from 6PM through 6AM”.

6. Run a command every Sunday at 5:30 PM

30 17 * * 0 /path/to/command

This command will run at 5:30 PM every Sunday. The 0 in the days of the week field is equivalent to saying “Sunday”.

7. Run a command on the 15th and last day of every month

0 0 15,* /path/to/command

This command will run at midnight (00:00) on the 15th and the last day of every month. The 15,* in the days field is equivalent to saying “the 15th and the last day of the month”.

8. Run a command every 30 minutes

*/30 * * * * /path/to/command

This command will run every 30 minutes. The */30 in the minutes field is equivalent to saying “every 30th minute”.

9. Run a command every year on Jan 1

0 0 1 1 * /path/to/command

This command will run at midnight (00:00) on January 1 every year. The 1 in the months field is equivalent to saying “January”.

10. Run a command every 6 hours

0 */6 * * * /path/to/command

This command will run every 6 hours. The */6 in the hours field is equivalent to saying “every 6th hour”.

Remember, it’s important to test your cron jobs to ensure they’re working as expected. Happy scheduling!

Leave a Comment