Cron job Setup

From PheonixSolutions
Jump to navigation Jump to search

Cron Job Setup in Linux

[edit]

Introduction

[edit]

A cron job is used for scheduling a task to be executed sometime in future. This is normally used to schedule a job that is executed periodically at the particular mention time.

Timing Syntax
This is the first part of the cron job string, as mentioned above. It determines how often and when the cron job is going to run.

It consists of five parts:

minute
hour
day of month
month
day of week
Here is an illustration:

https://wiki.pheonixsolutions.com/index.php/File:Vertopal_2c47e49938204b91b2e4621ed6ad59b0-media-image2.png

Asterisk

[edit]

you will see an asterisk (*) onbehalf of a number. This represents all possible numbers for that position. For example, asterisk in the minute position would make it run every minute.

We need to look at a few examples to fully understand this Syntax.

Examples:

[edit]

This cron job will run every minute, all the time:

1 * * * * [command]

This cron job will run at minute zero, every hour (i.e. an hourly cron job):

1 0 * * * [command]

This is also an hourly cron job but run at minute 15 instead (i.e. 00:15, 01:15, 02:15 etc.):

15 * * * * [command]

This will run once a day, at 2:30am:

30 2 * * * [command]

This will run once a month, on the second day of the month at midnight (i.e. January 2nd 12:00am, February 2nd 12:00am etc.):

0 0 2 * * [command]

This will run on Mondays, every hour (i.e. 24 times in one day, but only on Mondays):

0 * * * 1 [command]

You can use multiple numbers separated by commas. This will run three times every hour, at minutes 0, 10 and 20:

0,10,20 * * * * [command]

Division operator is also used. This will run 12 times per hour, i.e. every 5 minutes:

  • /5 * * * * [command]

Dash can be used to specify a range. This will run once every hour between 5:00am and 10:00am:

0 5-10 * * * [command]

Edit the crontab

You can edit the cron using the crontab -e it will open the vi editor we used this to add the cron.

To check what are the running cron job

You can used crontab -l by using this command we will see what are the running cron jobs.

Deleted the cronjob

You can use crontab -r by using this command we can delete the cron job.