Cron Job Scheduling by Examples

The “cron job” is the time-scheduling process that allows the user to automate commands or scripts. These commands or scripts execute as the cron job at a defined time sequence. It works on the “crond” daemon that runs the cron jobs systematically in the background.

The “cron jobs” help perform regular-based tasks such as deleting temporary/cache files, updating the system repositories, sending emails, generating the backup of important files, and much more. This guide enlists a large list of practical examples showing cron job scheduling at different intervals.

 The outcomes of this guide are listed here:

Cron Job Syntax

The working of the cron job relies on its generalized syntax that is written below:

* * * * * command

The crontab syntax contains the following parameters:

  • First (*): Specifies the time in “minutes(0-59)”
  • Second (*):  Represents the time in the “hours(0-23)”
  • Third (*):  Denotes the “days of the month(1-31)”
  • Fourth (*): Defines the “month(1-12)”
  • Fifth (*): Shows the  “day of the week(0-7)”
  • command: The defined cron job command that will run at the mentioned time.

Prerequisites

Before scheduling all the cron jobs at a defined time interval, some essential steps are carried out that are described below:

Step 1: Evaluate the cron Service

First, check the “cron” service status by executing the following “systemctl status” command in the terminal(Ctrl+Alt+T):

$ sudo systemctl status cron

In the above output, the green indicator verifies that the cron service is in an “active (running)” state.

Tip: Start the Crontab Service (If Inactive)

If the “cron” service is not active, then use the “systemctl start” command to start it and check its status:

$ sudo systemctl start cron

Step 2: Create the New Crontab File

Secondly, create the new crontab file as it is not available by default. For this purpose, use the following “crontab” command:

$ crontab -e

If the user is creating it for the first time, then it will ask to select the text editor. In this case, the first “/bin/nano” default editor is set by passing the “1” integer in the terminal.

The new crontab file “/tmp/crontab.6xeBOB/crontab * ” is created, as shown in the above image.

Cron Job Scheduling by Examples

This section carried out a list of cron jobs practical examples that will execute specified hours, minutes, months, or years without user interference.

Example 1: Schedule a Cron Job Every Minute

Scroll down the newly created “crontab” file and add the following line at the end to schedule the cron job every minute.

In this example, we have redirected the output of the “echo” command to a “New.txt” file in the “home” directory that will print out the specified message every minute:

* * * * * echo "Welcome to Linux at $(date)" >> $HOME/New.txt

Note: For executing the cron job at specified minutes read this article.

Example 2: Schedule a Cron Job Every Hour

To run the cron job “Extra.sh” every hour, specify the time in this way:

0 * * * * /home/itslinuxfoss/test/Extra.sh

The  “0 * * * *” value refers to the cron daemon for the execution of the “Extra.sh” cron job for every hour as shown below:

Example 3: Schedule a Cron Job Every Month

If the user wants to execute the cron job every month then use the below-mentioned command in this way:

0 0 1 * * /home/itslinuxfoss/sample.sh

The  “0 0 1 * *” value specifies that the script “sample.sh” will run at 12:00AM on the first of every month:

Example 4: Schedule a Cron Job at  Midnight

To run the cron job task every Sunday at midnight add the following line into the “crontab” file:

0 0 * * 0 /root/backup.sh

The  “0 0 * * 0” values are mentioned to perform the backup of the system at midnight on Sunday:

Example 5: Schedule a Cron Job Every Weekday

To schedule the cron job every weekday from“Monday-Friday” use the following command:

0 0 * * 1-5 /root/clearcache.sh

The “0 0 * * 1-5” will clear the cache by executing the “clearcache.sh” script status every weekday”:

Example 6:  Schedule a Cron Job Every Year

Execute the cron job every year using the following mentioned line:

0 0 1 1 * bin/check-db-status

The “0 0 1 1 *” will check the database status every at 12:00 Am midnight on the 1st January of every year:

Example 7: Schedule Multiple Cron Jobs

To schedule the multiple commands as a single cron job by separating them AND “&&” operator and the “;(Semi-Colon)” symbol.

The “&&” operator runs the next command when the first command executes successfully. However, the “;” colon runs each command independently.

Syntax Using “&&” Operator:

$ * * * * * /path/to/command_1 && /path/to/command_2 && ...../path/to/command_N

In this case, this syntax is used to execute the “ls” and “chmod” commands together every “5” minutes as shown below:

*/5 * * * * ls test && chmod +x test.sh

Syntax Using “;” Symbol:

$ * * * * * /path/to/command_1 ; /path/to/command_2 ; ..... /path/to/command_N

In this example, the crontab file holds the three commands joined with “;(Semi-Colon)” in the same cron job that will execute simultaneously for every 10 minutes:

*/10 * * * * rm File1.txt ; cat>NewFile.txt ; ls -l NewFile.txt

Note: Click here to read our detailed article to run multiple commands in the same cron job.

Alternative Method: Schedule the Cron Jobs Using Special Strings

The “five asterisks” placed in the cron job syntax can also be replaced by the pre-defined special strings. The objective of these “special strings” is the same as the “asterisks” as they schedule the cron job at specified time intervals without figuring out the list of logical numbers.

@rebootRuns the cron job once at startup.
@yearlyExecutes the command once every year at midnight of 1st January i.e (0 0 1 1 *)
@monthlySchedules all cron job tasks once a month, at midnight of the first day of the month i.e (0 0 1 * *)
@weeklyRuns every cron job once a week at midnight on Sunday i.e (0 0 * * 0)
@dailySchedules the cron job once a day at midnight i.e (0 0 * * *)
@hourlyHelps to perform a task at the beginning of every hour i.e (0 * * * *)

It reduces the valuable time that the user consumes while typing a time in numbers format. To use them with the cron job, specify the “@” symbol followed by a simple pre-defined string as shown below:

For Weekly

@ Weekly /home/itslinuxfoss/sample.sh

For Monthly

@monthly /home/itslinuxfoss/sample.sh

For Daily

@daily /home/itslinuxfoss/sample.sh

For Hourly

@hourly /home/itslinuxfoss/sample.sh

For Yearly

@yearly /home/itslinuxfoss/sample.sh

That’s all about various examples of cron job scheduling.

Conclusion

The “cron jobs” are defined in the “crontab” file that schedules the defined commands for execution periodically.  Access the “crontab” file via the “crontab -e” command. Once done, all the jobs will automatically be executed after every minute, hour, weekday, month, and year. This guide has provided numerous practical examples that schedule the cron jobs at defined time intervals.