Cron Expression to Execute Once a Year
Cron expression is a popular way to schedule tasks and automate mundane tasks in a Unix/Linux environment. It enables the user to specify the time interval to execute the command. A Cron expression consists of six fields separated by spaces:
┌───────────── second (0-59)
│ ┌───────────── minute (0-59)
│ │ ┌───────────── hour (0-23)
│ │ │ ┌───────────── day of the month (1-31)
│ │ │ │ ┌───────────── month (1-12 or Jan-Dec)
│ │ │ │ │ ┌───────────── day of the week (0-6 or Sun-Sat)
│ │ │ │ │ │
│ │ │ │ │ │
* * * * * *
Each field represents a particular unit of time and can accept multiple values separated by commas or ranges separated by hyphens. Cron expression supports various combinations of schedules like hourly, daily, weekly, monthly, or yearly.
In this article, we will learn how to create a cron expression that executes once a year.
Cron Expression for Once a Year
A Cron job can be created to run once a year on a specific date and time. The format of Cron’s yearly expression is:
“`0 0 1 1 *“`
This Cron expression reads as follows:
- The first ‘0’ represents the seconds
- The second ‘0’ represents the minutes
- The third ‘1’ represents the hour (1 AM)
- The fourth ‘1’ represents the day of the month (1st)
- The fifth ‘*’ represents the month
- The sixth ‘*’ represents the day of the week
So, this Cron expression will execute once on the 1st of January at 1 AM.
Let’s see how to set up a new Cron job with this Cron expression.
Setting Up a Cron Job
To set up a new Cron job, open the terminal window and enter the following command:
“`crontab -e
<pre><code class="line-numbers">This command will open the user’s crontab file in the editor.
To create a Cron job that runs once a year on the 1st of January at 1 AM, add the following line to the crontab file:
“`0 0 1 1 * /path/to/command argument“`
Replace `/path/to/command argument` with the command you want to execute.
For example, to execute a Python script, the cron job should look like:
“`0 0 1 1 * python3 /path/to/script.py
</code></pre>
Once the line is added, save the file and exit the editor.
This Cron job will execute once a year on the 1st of January at 1 AM.
<h2>Testing the Cron Job</h2>
To test the Cron job, we can change the Cron expression to run every minute temporarily.
“`* * * * * /path/to/command argument“`
This Cron job will execute every minute.
Once the line is added, save the file and exit the editor.
Wait for a minute and check the output generated by the command we have given as an argument. If the output is expected, you can change the Cron expression back to the original one.
Conclusion
Cron expression is a powerful tool to automate tasks in Unix/Linux environment. Creating a Cron job that executes once a year is an easy process. The Cron expression ‘0 0 1 1 *’ can be used to schedule tasks that run on the 1st of January at 1 AM. By following the instructions above, you can easily create a Cron job that runs once a year based on your task requirements.