How to Create Cron Job to Adjust Time Through Linux NTP Server

Create Cron Job to Adjust Time Through Linux NTP Server

In Linux base operating system daemon "Cron" is used to schedule the task automatically. Cron is also known as "Cron Job". In more simple words Cron is a tool to run a job and all of the jobs have to create in Crontab file. Crontab is a combination of Cron and Table (Cron+Table). Every users can create its own "Cron Jobs" to run the task automatically. Administrative user "root" can restrict to the other users to create "Cron Jobs", means if Administrative user "root" add other user in below mention file then user unable to create Cron Jobs. 

/etc/cron.deny

Cron Job is used to automate the process like synchronization of files, email message forwarding, automatic updates, run a specific task means adjust time with Linux NTP Server, data backup and many more.

Here we are creating a Cron Job to adjust time through CentOS Linux NTP Server in Oracle Linux 6.5 client machine. NTP stands for Network Time Protocol, it is a networking protocol that use for clock synchronization between the computer systems. The port of NTP "Network Time Protocol" is 123 and it is use User Datagram Protocol "UDP".

First create a script. you can create a script in any location. Here we are creating a script on directory "desktop" of user "root" because this script is being created by user "root".

Use command "cd" to enter in the directory "desktop", use command "ls" to list the current content in the directory and create the script "ntptime.sh" by use of editor "vi".

cd Desktop/
ls
vi ntptime.sh

Note: .sh is an extension of shell scripting file in Linux, it is used to create executable file in Bourne Shell.

using cron job to adjust time

Add the below mention lines in script "ntptime.sh". For edit the file press "insert".

#!/bin/bash
ntpdate -u 172.20.1.235
After add the above mention lines, press "esc" then save file by type ":wq".

using cron job to adjust time

To check the script type the below mention command. If it give the proper result it means it is working fine.

sh ntptime.sh

how to sync linux time with ntp server

In the above image you can see that the first time script is run it gives offset -30.32 seconds and second time it gives offset -0.000010 seconds. It means when first time script run, the difference between client machine time and server machine time was 30 seconds and then second time script run the difference between client machine time and server machine time is very minor that is -0.000010 seconds. It means script is working fine.

To check the current date and time of CentOS Linux machine type the below mention command.

date

how to create a bash script in linux shell

To check the script permission type the below mention command.

ll

how to create a bash script

In the above image you can see that only user "root" have read and write permission on script "ntptime.sh", group and other users have only read permission. To give execute permission of user "root" on script "ntptime.sh" type the below mention command.

chmod 744 ntptime.sh

how to create a bash script

To again check the permission of script "ntptime.sh", type the below mention command.

ll

bash script

In the above image you can see that only user "root" have read, write and execute permission on script "ntptime.sh", group and other users have only read permission.

To list the Cron Job, type the below mention command.

crontab -l

cron job

In the above image you can see that no cron job has created by user "root" till now.

To create a Cron Job, type the below mention command.

crontab -e

cron

Add the below mention line to run the script at 7:01am.

1 7 * * * /root/Desktop/ntptime.sh

how to create cron job to adjust time through ntp server

After save the file, crontab is successfully installing new crontab.

how to create cron job to adjust time through ntp server

To check the log messages that Cron Job has run successfully, enter in directory "/var/log/" by use of command "cd".

cd /var/log/

how to create cron job to sync time through ntp server

To list log files type the below mention command.

ls

how to create cron job to sync time through ntp server

To check log messages of file "Cron", type the below mention command.

more cron

how to create cron job to sync time through ntp server

In the below image you can see that the Cron Job of script "ntptime" has executed successfully at 7:01am.

cron job

Thanks for Read this Article

Comments