10 Cron Scheduling Task Examples in Linux


Linux is an operating system that is widely used in servers, supercomputers, and mobile devices. One of the powerful features of Linux is the ability to schedule tasks using a tool called Cron. Cron is a time-based job scheduler in Linux that allows users to run scripts or commands at specified intervals. In this article, we will discuss Cron scheduling task examples in Linux.

What is Cron?

Cron is a time-based job scheduler in Linux that allows users to automate tasks at specified intervals. It can run scripts or commands at a specific time or a set of times. Cron is essential for system administrators because it automates tasks such as backups, system updates, and log rotation.

Cron uses a configuration file called crontab, which is located in the /etc/ directory. The crontab file contains a list of jobs that Cron will execute. Each job consists of a line with six fields separated by spaces. The fields represent the minute, hour, day of the month, month, day of the week, and command to be executed.

The following are the six fields used in the crontab file −

  • Minute − This field represents the minute of the hour (0-59).

  • Hour − This field represents the hour of the day (0-23).

  • Day of the month − This field represents the day of the month (1-31).

  • Month − This field represents the month of the year (1-12).

  • Day of the week − This field represents the day of the week (0-6). Sunday is represented by 0 or 7.

  • Command − This field represents the command to be executed.

Cron Scheduling Task Examples

Running a Script Every Minute

To run a script every minute using Cron, add the following line to the crontab file −

* * * * * /path/to/script.sh

This line will run the script every minute, regardless of the hour, day, month, or day of the week.

Running a Script Every Hour

To run a script every hour using Cron, add the following line to the crontab file −

0 * * * * /path/to/script.sh

This line will run the script at the beginning of every hour, regardless of the day, month, or day of the week.

Running a Script Every Day

To run a script every day using Cron, add the following line to the crontab file −

0 0 * * * /path/to/script.sh

This line will run the script at midnight every day, regardless of the month or day of the week.

Running a Script Every Week

To run a script every week using Cron, add the following line to the crontab file −

0 0 * * 0 /path/to/script.sh

This line will run the script at midnight every Sunday, regardless of the month.

Running a Script on Specific Days of The Week

To run a script on specific days of the week using Cron, add the following line to the crontab file −

0 0 * * 1-5 /path/to/script.sh

This line will run the script at midnight from Monday to Friday, regardless of the month.

Running a Script on Specific Days of The Month

To run a script on specific days of the month using Cron, add the following line to the crontab file −

0 0 1,15 * * /path/to/script.sh

This line will run the script at midnight on the 1st and 15th of every month.

Running a Script on a Specific Date and Time

To run a script on a specific date and time using Cron, add the following line to the crontab file −

0 12 25 12 * /path/to/script.sh

This line will run the script at 12:00 PM on December 25th every year.

Running a Script Every 10 Minutes

To run a script every 10 minutes using Cron, add the following line to the crontab file −

*/10 * * * * /path/to/script.sh

This line will run the script every 10 minutes, regardless of the hour, day, month, or day of the week.

Running a Script Every 30 Minutes

To run a script every 30 minutes using Cron, add the following line to the crontab file −

*/30 * * * * /path/to/script.sh

This line will run the script every 30 minutes, regardless of the hour, day, month, or day of the week.

Running a Script Every 2 Hours

To run a script every 2 hours using Cron, add the following line to the crontab file −

0 */2 * * * /path/to/script.sh

This line will run the script every 2 hours, starting at midnight, regardless of the day, month, or day of the week.

Running a Script on Reboot

To run a script on reboot using Cron, add the following line to the crontab file −

@reboot /path/to/script.sh

This line will run the script every time the system is rebooted.

Running a Script on Weekdays Only

To run a script on weekdays only using Cron, add the following line to the crontab file −

0 0 * * 1-5 /path/to/script.sh

This line will run the script at midnight from Monday to Friday, regardless of the month.

Running a Script on Weekends Only

To run a script on weekends only using Cron, add the following line to the crontab file −

0 0 * * 6,7 /path/to/script.sh

This line will run the script at midnight on Saturday and Sunday, regardless of the month.

Running a Script Every 5 Hours

To run a script every 5 hours using Cron, add the following line to the crontab file −

0 */5 * * * /path/to/script.sh

This line will run the script every 5 hours, starting at midnight, regardless of the day, month, or day of the week.

Running a Script Every 15 Minutes Between 9am and 5pm

To run a script every 15 minutes between 9am and 5pm using Cron, add the following line to the crontab file −

*/15 9-17 * * * /path/to/script.sh

This line will run the script every 15 minutes between 9am and 5pm, regardless of the day or month.

Running a Script Every Hour Between 9am and 5pm on Weekdays

To run a script every hour between 9am and 5pm on weekdays using Cron, add the following line to the crontab file −

0 9-17 * * 1-5 /path/to/script.sh

This line will run the script every hour between 9am and 5pm on weekdays, regardless of the month.

Running a Script Every 5 Minutes on Weekends

To run a script every 5 minutes on weekends using Cron, add the following line to the crontab file −

*/5 * * * 6,7 /path/to/script.sh

This line will run the script every 5 minutes on Saturday and Sunday, regardless of the month.

Running a Script Every Month on The First Day

To run a script every month on the first day using Cron, add the following line to the crontab file −

0 0 1 * * /path/to/script.sh

This line will run the script at midnight on the first day of every month.

Conclusion

Cron is a powerful tool in Linux that allows users to automate tasks at specified intervals. It is essential for system administrators because it automates tasks such as backups, system updates, and log rotation. Cron uses a configuration file called crontab, which contains a list of jobs that Cron will execute. Each job consists of a line with six fields separated by spaces. The fields represent the minute, hour, day of the month, month, day of the week, and command to be executed.

In this article, we discussed Cron scheduling task examples in Linux, including running a script every minute, running a script every hour, running a script every day, running a script every week, running a script on specific days of the week, running a script on specific days of the month, running a script on a specific date and time, running a script every 10 minutes, running a script every 30 minutes, and running a script every 2 hours.

Updated on: 27-Apr-2023

334 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements