Saturday, 28 January 2017

Cron Job in linux

Cron allows linux or unix users to run scripts at a given time and date. We can schedule any script be executed periodically.

Linux/Unix provide the crontab inbuilt utility to schedule any script.

Let's go through an example and see how crontab would be setup with the help of an example.

Considering we have a java file, and this is wrapped up in a jar file. We are gonna to schedule that jar using crontab.

Create a shell script file(test.sh) with the entry

java -cp .:test.jar Scheduler

Where

test.jar: jar file name
Scheduler: Main class

crontab command is used to edit/list crontab:

How to edit the crontab

To edit a crontab entries, use crontab -e as shown below. By default this will edit the current logged-in users crontab.


Crontab in linux


How to view the crontab

To view your crontab entries type crontab -l from your linux  account as shown below.



Crontab in linux


Syntax of Crontab:

1 2 3 4 5 /root/test.sh

Where,

1: Minute (0-59)
2: Hours (0-23)
3: Day (0-31)
4: Month (1-12)
5: Day of the week(0-6)


 /root/test.sh – Script or command name to schedule


Let's create a crontab. We are gonna to schedule test.sh file that will run daily midnight. Hit the crontab -e to install the crontab and add the entry 1 0 * * * /root/test.sh and save the file.

Crontab in linux

Now test.sh file will run daily midnight at 00:01AM..

That't it !!

No comments:

Post a Comment