Click here to Skip to main content
16,016,962 members
Articles / Operating Systems / Linux

Using Cron Job to Adjust Time

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
24 May 2017CPOL3 min read 18.9K   2   4
Using Cron job to adjust time

Introduction

Recently in an application I developed, I noticed I received a unauthorized response for an API GET to a RESTful Service which I developed in Symfony. I was baffled as to why I would all of a sudden get an unauthorized response, since the app was previously working. In this case, my app sends X-WSSE headers to authenticate using the WSSE protocol, which I described previously in my REST Easy With Symfony article.

What I found is that there was drift occurring in the virtual machine guest, and it was losing up to 6 minutes in a month, which was a large amount of time drift.

Adjusting the Time

I found the simplest solution to adjust the time in the guest was to run the following command:

ntpdate time.nist.gov

The above command just runs the “ntpdate” command with the parameter “time.nist.gov”, which essentially polls an ntp server to synchronize time on the guest. One way to use this command to compensate for the time drift, is to occasionally run the command; however, this is not feasible, since I may not check the machine(s) very often.

Cron to the Rescue

Cron is a system scheduler that allows you to run a command or script at very specific time(s). Most Linux operating systems have the command “crontab” which makes it easy to edit and list the cron scheduler. To edit your own (that belongs to the user role you are logged in as) cron scheduler, run the following command:

crontab -e

The “-e” parameter specifies “edit” and opens the default crontab editor (which is normally vi). You can then use the standard vi editor command to edit the cron file as to your required needs. If you need to see how the cron scheduler is configured, then run the following command:

crontab -l

The “-l” parameter specifies “list”, so it lists the cron scheduler settings.

Format of the Cron File

The cron file that gets created normally resides in “/var/spool/cron”, so you could run:

sudo /var/spool/cron

Which would list all the cron files for all the users on the system. The above presumes you have been added to the “sudo” users list, otherwise you would not be able to run the command.

The following diagram shows the format of the cron file:

Cron_Format

Each item is separated by a space, so the above file runs the “ntpdate” command at “0” minutes” and “0” hours (midnight), everyday of the month, every month, and every day of the week.

Running as the Root User

In my case, I normally log onto this system as a “user” and not the “root” user. The “ntpdate” also needs to be run as the root user. So I have to run it from the command line like so:

sudo ntpdate time.nist.gov

So the question then is, how do you do this in cron? If you put the above in your own cron scheduler using “crontab -e”, it will fail when it tries to run, since you are not the root user.

The solution is to run “crontab” with sudo. So you would use this command:

sudo crontab -e

This creates a cron file for the “root” user.

Running More Frequently

I noticed when I ran the above cron scheduler (root cron job) only at midnight, the clock didn’t synchronize fully. So I wanted to increase the number of times it runs. To do that, run “sudo crontab -e” and let’s make it run 3 times a day:

0 0,08,16 * * * sudo ntpdate time.nist.gov

The above changes will now run everyday at midnight, 8:00 AM, and 4:00 PM.

Verifying Cron Job Ran

To check that the above cron job actually ran successfully, you can check the cron log file which is normally located in the folder “/var/log” and will have the file name “cron”. If your system uses log rotate, then older logs will have a timestamp appended; for example “cron-20170522”.

To verify, just open the cron log file and search for “ntpdate”, and you should see that the command is run by the root user.

Hopefully, this helps someone that needs to run a scheduled command or script.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Taft College
United States United States
I’m a software developer. Currently I’m working at Taft College as a Programmer.

Comments and Discussions

 
Questionhazards of adjusting time Pin
ddyer25-May-17 7:33
ddyer25-May-17 7:33 
AnswerRe: hazards of adjusting time Pin
Alvin Bunk29-May-17 13:53
professionalAlvin Bunk29-May-17 13:53 
This is not a good response. Since "ntpd" is commonly used to adjust the time on a system throughout the day on most Linux system (not on RHEL though).

"Good" software should be able to adjust for time changes. For example, look at the "date" (day of month) not the "time" (time of day).

Also, I have a genuine reason to ensure the time is syncronised.
QuestionWhy not just use ntpd? Pin
Peter_in_278025-May-17 3:35
professionalPeter_in_278025-May-17 3:35 
AnswerRe: Why not just use ntpd? Pin
Alvin Bunk29-May-17 13:42
professionalAlvin Bunk29-May-17 13:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.