Script to adjust system time


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to adjust system time
# 1  
Old 02-25-2011
Script to adjust system time

Hello everyone,

I am trying to write a script that will accomplish the following:

- query current system time and store result into a variable
- wait for some amount of time, say 300 seconds
- reset system time to earlier queried time + 1 second

I did some basic shell scripting (CSH mainly) and took an intro to perl class in 2000 but have not touched either since, and as a result I really have no idea how to accomplish this task. I'm also not really sure how to do the math on the time since the systemsetup -settime requires hh:mm:ss, so at 59 seconds I'll run into a problem Smilie

I'm currently using OS X 10.6.6 and /bin says I have access to bash, csh, ksh, tcsh, and zsh. I have no preference for which shell the script runs in, although echo $SHELL says my current default is bash, if that makes any difference.

I would also like to use argv (or something similar) to define how many cycles I'd like to process to repeat and possibly to define the wait time as well instead of hard coding these into the script.

Thanks in advance!

Xaiu
# 2  
Old 02-26-2011
The right way to ajust the system time is to configure NTP
also see pool.ntp.org
This User Gave Thanks to ctsgnb For This Post:
# 3  
Old 02-26-2011
Sounds like the OP wants to slow down the system clock, not keep it accurate. NTP would keep it accurate.
This User Gave Thanks to KevinGB For This Post:
# 4  
Old 02-26-2011
KevinGB is on the right track - the script isn't meant to adjust the system time to a reference time source to keep it accurate. I have an application that references the system time and I want to run that application for a set period of time, from say 12:00:00 to 12:05:00, tabulate the output, and then reset the system clock to 12:00:01 and run the application from 12:00:01 to 12:05:01 and compare the output to the previous run.

Actually, since I originally posted I realized that I don't need to query the current time at all. Instead I can just pass a clock time, wait period (in seconds), next cycle increment (in seconds), and an exit time into the script. This should get me around quite a number of the problems I was initially having and will hopefully simply the script down to something I can muddle my way through. Anyhow, I could do something like:

> script 12:00:00 300 1 12:00:05

which would hopefully:

- set the system time to 12:00:00
- wait for n seconds
- increment the input system time by increment (in this case 1 second)
- compare the result to the end time (passed in from script) and if new system time is less end time
- reset system time and loop to wait for n seconds

so in this scenario I should get 5 cycles that lasted for 5 minutes each and my timestamp on the data that is being collected would show:

1st cycle: 12:00:00 - 12:05:00

2nd cycle: 12:00:01 - 12:05:01

3rd cycle: 12:00:02 - 12:05:02

Thanks for your responses!

Last edited by xaiu; 02-26-2011 at 07:49 PM.. Reason: address additional comments left by another response
# 5  
Old 02-26-2011
It is not advisible to change the system time in the manner proposed in post #1. Hard setting the date/time should only be done in single user mode armed with full knowledge of the effects on programs such as System Accounting and any Database Engines you may have.

What are you trying to do? We seem to have a proposed solution without knowing the problem.

If your system clock is a little bit fast, there should be a clock drift function (see "man date"). Or if you have access to a Time Server, use NTP as ctsgnb advises.

(Post drafted before seeing post #4). There has to be a better way which does not involve messing with the system clock.

Last edited by methyl; 02-26-2011 at 07:57 PM..
This User Gave Thanks to methyl For This Post:
# 6  
Old 02-26-2011
Methyl, yes, great point.

This is a single user stand alone system running a custom simulation application for which I am collecting results at 1 second intervals. The application takes into account the time of day, which it gets from the system time. IE my data output from 12:00:00 - 12:59:59 will be different than the data output from 13:00:00 - 13:59:59, even though I have not changed any of the application parameters.

Since I can't change the behavior of the application (it getting it's simulation time from the system clock) I have been running the application for a set period of time and then re-adjusting the system clock by hand. I'm looking for a way to let the application run continuously, but appear as if it had been run for a set number of cycles with each cycle lasting a set period of time, and each cycle beginning from an incremental offset.

Hope that helps clarify the problem I'm trying to solve with the script.
# 7  
Old 02-27-2011
Woudn't it be possible to play with the Timezone ?
Something like
Code:
TZ=$(($TZ+1)) && ./appli

or (FreeBSD)
Code:
$ export TZ=/usr/share/zoneinfo/Etc/UTC


Code:
$ date
Sun Feb 27 08:07:58 UTC 2011
$ export TZ=/usr/share/zoneinfo/Etc/GMT+1
$ date
Sun Feb 27 07:08:14 GMT+1 2011

---------- Post updated at 09:18 AM ---------- Previous update was at 09:11 AM ----------

[ctsgnb@shell ~/sand]$ export TZ=/usr/share/zoneinfo/Etc/UTC
Code:
$ date +'%H:%M:%S' && export TZ=/usr/share/zoneinfo/Etc/GMT+1 && date +'%H:%M:%S'
08:18:08
07:18:08
$

This User Gave Thanks to ctsgnb For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

Time: Hwclock and System Time

Hey everyone. Upon studying linux trying to learn it inside and out, I'm reading about the issue of time. Hardware clock time vs the more commonly referenced System Time. What causes the two to grow apart, and what causes the time itself to stray away from UTC? at present my clock is a second and... (1 Reply)
Discussion started by: Lost in Cyberia
1 Replies

2. Shell Programming and Scripting

Additional time to system time

Hi All, Is there any command to add additional time to date command. I need to add 5 hours to the present system time. I am getting the time by using date command. WORKFLOW_START_TIME=`date +%m/%d/%Y\ %H:%M:%S` Thanks (8 Replies)
Discussion started by: nag_sathi
8 Replies

3. Shell Programming and Scripting

what would a script include to find CPU's %system time high and user time high?

Hi , I am trying to :wall: my head while scripting ..I am really new to this stuff , never did it before :( . how to find cpu's system high time and user time high in a script?? thanks , help would be appreciated ! :) (9 Replies)
Discussion started by: sushwey
9 Replies

4. Solaris

System time and Cron time stamp not matching

On Solaris 10 server the system date won't match with the timestamp on files created by a cron jobs, Please help here is what i get when i check for system date infodba-ie10ux014:/tcpdv1_ie10/tcadmin/bin\n\r-> date Tue Apr 24 15:27:43 GMT 2012at same time i executed a cron job, and checked... (4 Replies)
Discussion started by: karghum
4 Replies

5. Solaris

can't adjust time : not owner

Hi experts, I m new to solaris. I am getting following errror in /var/adm/messages file on one of the solaris 10 zones - xntpd: Can't set time of day: Not owner xntpd daemon is online. Any help on this ? (3 Replies)
Discussion started by: sunadmin
3 Replies

6. Shell Programming and Scripting

Script to adjust network settings

Hello, Newbie question on scripting - I'm looking to create a simple script that will work on RHEL5 that will adjust the network settings: ip address, default gateway, and subnet mask. If anything else needs to be done (service network stop / start) or should be done to make settings active - I... (4 Replies)
Discussion started by: rojizo
4 Replies

7. Solaris

getting time independent of system time in solaries

i am using function gethrtime() in sun solaries to get the time independent of the system time.Problem with this function is if we restart the system time will change to '0'.is there any other way to resolve this problem. thanks & regards suresh (3 Replies)
Discussion started by: suresh_rtp
3 Replies

8. Shell Programming and Scripting

System time comparison to fixed defined time

I have a requirement of checking the current system time and performing certain actions in a shell script. example: if the current system time is greater than 1400 hrs, then perform step 1,2,3 if the current system time is greater than 1000 hrs, then perform step 1,2 if the current system time... (2 Replies)
Discussion started by: zainravi
2 Replies

9. Shell Programming and Scripting

How to adjust spacing

Is there a way to adjust spacing of a line using k shell? e.g I have a file below $ cat file1 AAA BBB CCC A B C AAAA BB CC I want each word to be adjusted with spaces to have 10 character length like below: AAA BBB CCC A B C AAAA BB CC Any... (4 Replies)
Discussion started by: stevefox
4 Replies

10. Shell Programming and Scripting

Adjust the db script

Enclosing a script that is used everyday for database shutdown. In here you will find some code that checks oracle version. That part is very unnecessary since we use only 8.1.7 and will never go back.. Can anyone help me by modifying the code, to never use that part and readjust so script does... (1 Reply)
Discussion started by: ST2000
1 Replies
Login or Register to Ask a Question