Cron Expression to Execute Once a Year

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.

Like(0)
Linux Login Logout Command
Linux login commandLinux logout commandLinux nologin commandLinux exit commandLinux sulogin commandLinux rlogin commandLinux poweroff commandLinux ctrlaltdel CommandLinux shutdown commandLinux halt commandLinux reboot commandLinux init commandLinux runlevel commandLinux telinit command
Linux File Management Command
Linux cat commandLinux tac commandLinux nl commandLinux more commandLinux less commandLinux head commandLinux tail commandLinux rev commandLinux fold commandLinux fmt commandLinux expand commandLinux pr commandLinux sort commandLinux uniq commandLinux cut commandLinux comm commandLinux diff commandLinux join commandLinux diff3 commandLinux cmp commandLinux colrm commandLinux paste commandLinux mkdir commandLinux tr commandLinux split commandLinux csplit commandLinux tee commandLinux unexpand commandLinux patch commandLinux awk commandLinux sed commandLinux od commandLinux pwd commandLinux cd commandLinux ls commandLinux dir commandLinux dirs commandLinux touch commandLinux rmdir commandLinux cp commandLinux mv commandLinux rm commandLinux install commandLinux tmpwatch commandLinux file commandLinux du commandLinux wc commandLinux tree commandLinux cksum commandLinux md5sum commandLinux sum commandLinux dirname commandLinux mkfifo Command
Cron Expressions
Cron Expression to Run Every Day at 12 PMUnderstanding Vue Cron ExpressionsUnderstanding JS Cron ExpressionsA Comprehensive Guide to Cron Expressions for Scheduled TasksUnderstanding Linux Cron ExpressionsUnderstanding Quartz Cron ExpressionsCron ExpressionCron Time ExpressionCron Expression ParsingCron Expression: Executing a Task Every SecondCron Expression for Every Minute ExecutionCron Expression to Execute Every 10 MinutesCron Expression: Executing Every HourCron Expression to Execute Once a YearCron Expression: How to Schedule a Task to Run Daily at Midnight?