Cron Time Expression
Cron time expression is a syntax that is used to describe when a task should be executed. These expressions are used in Linux and Unix systems to schedule jobs or tasks that run automatically at specific intervals, times, or dates. In this article, we will discuss what cron time expression is, how to read and write it, and some examples of using it in shell scripts.
Understanding Cron Time Expression
Cron time expression is made up of six fields that define the time and date when a task should be executed. These fields are separated by spaces, and the order of the fields is as follows:
* * * * * command to be executed
- - - - -
| | | | |
| | | | +----- day of the week (0 - 6) (Sunday=0)
| | | +------- month (1 - 12)
| | +--------- day of the month (1 - 31)
| +----------- hour (0 - 23)
+------------- min (0 - 59)
Each field is an integer or a special character that represents a specific time value. Here is a brief description of each field:
min
: specifies the minute of the hour (0-59).hour
: specifies the hour of the day (0-23).day of the month
: specifies the day of the month (1-31).month
: specifies the month of the year (1-12).day of the week
: specifies the day of the week (0-6), where Sunday = 0 and Saturday = 6.command
: specifies the command to be executed.
Asterisk () means that the field is any value. For example, the expression “ * * * * command” means that the command will be executed every minute of every hour, every day of the month, every month, and every day of the week.
Another special character is the forward slash (/), which is used to specify a step value. For example, the expression “*/2 * * * *” means that the command will be executed every two minutes. The slash character can also be used to specify a range of values, such as “2-6/2” means 2, 4, and 6.
Writing Cron Time Expression
To create a valid cron time expression, all six fields must be specified with integer values or special characters. Here are some examples of how cron time expression is written:
- “0 0 * * *”: This expression means that a command is executed at 12:00 AM every day.
- “30 8 * * 1-5”: This expression means that a command is executed at 8:30 AM from Monday to Friday.
- “0 */2 * * *”: This expression means that a command is executed every two hours.
- “0 0 1,15 * *”: This expression means that a command is executed at 12:00 AM on the 1st and 15th of every month.
- “0 0 * * 0”: This expression means that a command is executed at 12:00 AM every Sunday.
Using Cron Time Expression in Shell Scripts
To use cron time expression in a shell script, we need to define the cron expression as a string and then use the cron
module provided by the python-crontab
package to add the command and schedule it. Here is an example:
from crontab import CronTab
# create a cron tab
cron = CronTab(user='user')
# add a new cron job
job = cron.new(command='python /home/user/script.py')
# set the cron expression
job.setall('0 0 * * *')
# write the cron job to the cron tab
cron.write()
In this example, we create a new cron tab for the user “user” and define a new job that executes the command “python /home/user/script.py”. We set the cron expression to execute the command at 12:00 AM every day and write the job to the cron tab. The write()
method saves the job to the user’s crontab file.
Conclusion
Cron time expression is a powerful tool for scheduling tasks in Linux and Unix systems. By understanding its syntax and using it in shell scripts, we can automate repetitive tasks and improve our productivity. Remember to always test your cron expressions before deploying them to a production environment to avoid unexpected and unwanted execution.