What is a cron job

What is a cron job? | Crontab in Linux with practical examples

What is a cron job? :

I have written set of unix articles as well as Sql articles which gives user the information of different concepts of SQL as well as Unix.I ask my colleagues about suggesting any interesting topic on Linux.He answered ,” What is a cron job?”.So I am writing this article on What is a cron job?.I will give the real world examples about What is a cron job?. This article helps the user with linux who wants to know about the scheduling techniques of unix.A cron job is very important unix utility which gives user to schedule different commands of unix or different scripts of unix on specific date and time. Now a days there are so many project which needs to schedule different set of commands.Its a part of automating the commands of unix.

The cron jobs are very important utilities which automates repetitive tasks.

Crontab command with real examples :

The Crontab command is responsible to schedule different jobs in the unix. In this section i would like to explain more about the Crontab command in unix with different examples which is used in industries.This will give users deep idea about What is a cron job?.I have already explained about at command in unix which also uses for scheduling. Here in this article i will focus on what is a cron job?User must have knowledge of unix commands to schedule different Cron jobs.

Real life example of a cron job :

A cron job is basically used to schedule set of commands. In server side user needs to run the command to clean up the unwanted files regularly.So in that case user needs to run set of commands on regular basis.In spite of running the set of commands regularly if user sets the cron job on specific time that commands will run when that cron job is run.The user need not run the set of commands on regular basis.

The crontab command is used to create or edit or maintain the list of cronjobs. Each user have its own cron tab file and which will not be edited directly. User needs to use the crontab command for editing crontab file.User can set up the own cron jobs also by setting up the crontab command.In this section i will give you how to use crontab command for scheduling different files or scripts.

How to start a cron?

I would like to start with how to start a cron. Cron needs to be started only once. That’s the reason cron is called as a daemon.Now a days the crond is installed automatically and the code of the same is put in to the startup script of unix installation.There is a way to check the cron is running or not on the system.Use following command to check whether crond is running or not.

$ps aux | grep crond

The above command will check whether crond program is installed on the system or it is not installed.Here is the question if cron utility is not running on the system what to do?

1.Add crond in your startup script and reboot :

If the crond utility is not installed on the system then user needs to add the crond command in startup program. When you reboot the system the crond program will execute and utility in been installed.As i said that crond is daemon user needs to install it once.

2.Add crond without startup script:

If user dont want to reboot system.Just user need to type crond with its root log in. Here just make sure that user is logged in with root.

Root@xxx$crond

The above command will execute the crond command and user is now able to use the crontab command.

How to use cron with crontab command?

There is always a question in mind of users how to use cron with crontab command.There are multiple ways to use cron. User will find different sub-directories in /etc folder of Linux. These directories are cron.hourly, cron.daily ,cron.weekly and cron.monthly.

1.cron.hourly : If user put scripts in this folder then the scripts will run on hourly basis.

2.cron.daily :  If user put scripts in this folder then the scripts will run on daily basis.

1.cron.weekly: If user put scripts in this folder then the scripts will run on weekly basis.

2.cron.monthly:  If user put scripts in this folder then the scripts will run on monthly basis.

You will find crontab directory etc/ folder.The crontab will show as follows.

$cat /etc/crontab

SHELL=/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin

MAILTO=root

HOME=/

1.SHELL :

SHELL is the ‘shell script’ cron runs under. If unspecified, it will default to the entry in the /etc/passwd file.

2.PATH :

PATH is the path which shows the search path for the cron.

3.MAILTO :

This is most important part of crontab utility. MAILTO option gives you chance to configure the mail and it will send the output of each and every command to the mail configured in MAILTO option.

4.HOME:

Home is the home directory used for crontab.

Different Options needs to be used in Crontab command :

When user have question in mind What is a cron job? and How to run the Cron job using crontab command? this is the most complex part of it.The cron tab command has series of fields and user needs to use different fields together with crontab command. There are following 7 important fields which needs to be used with crontab command :

Syntax :

minute hour DayofMonth month DayofWeek user cmd

1.minute :

minute field controls what minute of the hour the command will run on.This field is between 0 to 59. It indicates each minutes which always controls the minutes.

2.Hour :

Hour field is used to control the hours. It has 0 to 23 values where 0 indicates the midnight.

3.DayofMonth :

This is the Day of Month, that you want the command run on, e.g. to run a command on the 10th day of each month, the dom would be 10.

4.month:

This is the month a specified command will run on, it may be specified numerically (0-12)

5.DayofWeek :

This is the Day of Week that you want a command to be run on, it can also be numeric (0-7) or as the name of the day (e.g. mon).

6.User :

This is the user who runs the command.

7.Cmd :

Cmd stands for command which user wants to run.This field contains multiple spaces or multiple words.

If user do not want to specify the value of the field then it needs to be filled with * special character. I will give you some examples which explains about using crontab.

Multiple Examples of using fields in crontab :

015 * * * * root echo “This command is run at five mins past every hour”

38 8 * * * root echo “This command is run daily at 8:38am”

17 21* * * root echo “This command is run daily at 9:17 pm”

00 6 * * 1 root echo “This command is run at 6 am every Monday”

42 4 19 * * root echo “This command is run 4:42 am every 19th of the month”

01 * 18 09 * root echo “This command is run hourly on the 18th of september”

 

These are above different examples of crontab where user needs to specify the command or set of commands with different fields. The * used to indicate when user dont want to use that option.In following section i would like to give you multiple examples of crontab.

User can use different crontab keywords as well.

Keyword    Equivalent
@yearly    0 0 1 1 *
@daily     0 0 * * *
@hourly    0 * * * *
@reboot    Run at startup.

Example 1 : How to schedule cron job for Specific time

There are so many situations where user needs to schedule job for running set of commands on specific time.Let us take a real world example for the same.If user wants to take backup of the files of there project at every week on specific time .

30 11 10 09 * /home/complexsql/Backup

The above configurations will schedule the backup on weekly basis from 10th September at 11:30 AM.

30 : This indicates minutes.

11: This field indicates hour

10: This field indicates day or date.

09 : This field indicates month. 09 stands for month of September.

Example 2 : Schedule Job on every 5 minutes

There are some situations where user needs to schedule job for every five minutes.The following configuration is useful in that situations.

To schedule job on every minute :

* * * * * Set of Commands

To schedule job on every 5 minutes :

*/5 * * * * Set of Commands

User can use so many configurations to schedule the jobs.When you specify */5 in minute field means every 5 minutes.When you specify 0-30/2 in minute field mean every 2 minutes in the first 30 minute.

Example 3 : Schedule job in more than one time in a day

There are so many situations where user needs to schedule job for more that one time in a day. These situations comes when user needs to take incremental backup.To schedule job in more that one time in a day just use comma operator in between two times. Lets say user wants to schedule backup at 11 AM and 6 PM for everyday then following configuration needs to be done.

0 0 11, 18 * * * /home/complexsql/bin/complexsql-backup

The above command will take backup on everyday at 11 AM and 6 PM. Here the 0 0 indicates the 0 minutes.And other 3 stars indicates everyday,every week and every month.

So the above command is used to take backup of complexsql on 11 AM and 6 PM everyday.

Example 4 : Schedule job on Weekdays

There are certain situations where user needs to schedule the cron jobs on weekdays only.Lets say your company is working in some office hours and only on weekdays and you need to check the status of the backup files.The following configuration is useful in that case.

00 09-18 * * 1-5 /home/complexsql/bin/backup-status

The above configuration indicates that the job should run in between 9 to 6 and on first five days.Here 1-5 indicates Monday to Friday.

Example 5 : Happy New Year Job

There are situations where user needs to send the happy new year messages to first minute of every new year. User can achieve this using following configuration

0 0 1 1 * Send_mails_Shell_script

@yearly Send_mails_Shell_script

Instead of using 0 0 1 1 * and making complicated configuration User will be able to use @yearly keyword.

Same as above user can schedule different cron jobs at beginning of every month and beginning of every day. User needs to use @monthly and @yearly keyword for the same.

@monthly /home/complexsql/bin/monthly-backup-status

@daily /home/complexsql/bin/daily-backup-status

These are some important real world examples of Scheduling cron job. It really gives you idea about What is a cron job? and what are its usages. Hope you like this article on What is a cron job? If you like this article on What is a cron job? or if you have any issues or concerns with the same kindly comment it in to comment section.

Leave a Reply

Your email address will not be published. Required fields are marked *