Script to adjust system time


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to adjust system time
# 8  
Old 02-27-2011
Quote:
single user stand alone system
More relaxed about this now.

Be careful. As you appear to be only changing the time (not the date) there is likely to be less impact. The unix "date" command will accept just a time parameter. Be aware of post-dated files.



During Y2K testing I had to set up a "Groundhog Day" system which repeated the same given day again-and-again and needed to save and reset (or replace) system accounting files, system logs, software licence files etc. etc. and deal with timestamps on post-dated files (or remove them as appropriate). This was not trivial.

One awful database crash I attended was caused by someone changing the system clock by a couple of minutes.
This User Gave Thanks to methyl For This Post:
# 9  
Old 03-05-2011
So I've been making some progress on my question and I was actually playing around with the date command before I saw your response. On mac OS I have been using the command:

systemsetup -gettime which returns Time: HH:MM:SS

so I've been trying to figure out how to strip Time: and assign hours=HH minutes=MM seconds=SS. I could then:

sh script.sh 300 1 10
- get current system time with systemsetup -gettime
- strip Time: and convert the system time to seconds
- initialize cycle count to 1 and
- while count less than 10 (passed in from command line as $3) do
- wait for 300 seconds (passed in from command line as $1)
- add the increment 1 (passed in from command line as $2) to system time converted to seconds as new_systemtime
- convert new_systemtime back to HH:MM:SS format
- reset system time to new_systemtime by systemsetup -settime $new_systemtime
- increment cycle_count by 1

I was able to use awk -F: to print each number independently. It's ugly, but:

systemsetp -gettime ; a=$(systemsetup -gettime) ; echo $a | awk -F: '{print $2":"$3":"$4}'
Time: 22:32:59
22:32:59

but then I got stuck at how to shove $2 $3 and $4 into the variables hours, minutes, and seconds instead of printing them.

Anyhow, any tips would be greatly appreciated on how to go about each of those steps, or if I'm just going about it all the wrong way, that'd be good to know too Smilie

Last edited by xaiu; 03-05-2011 at 02:50 AM.. Reason: clarification
# 10  
Old 03-05-2011
Maybe try saving the current time in a file in a private directory with the name of the file the value of HHMMSS . Then (after 300 seconds or whatever) use the name of the file in a unix "date" command to reset the time as required.
When working with Groundhog Day software testing the advantage of using a file instead of a Shell environment variable is that it survives system reboots. Also, you can alter the name of the file to whatever you want.
This User Gave Thanks to methyl For This Post:
# 11  
Old 03-13-2011
Okay,

So I fumbled through it and I've managed to get my script running, all except re-setting the system time. I have $h $m $s and I'm trying to make a single variable $set_time that takes the form $h:$m:$s. Everything I've tried I just end up with $h $m $s. How do I get a colon included in the variable?

Thanks! Here's the whole thing, in case it helps.

Code:
# Call this script with parameters for wait_time, increment, and cycle_count
#
# example sh test.sh 300 1 10
# this will sleep for 300 seconds, increment the start time by 1 second, and run
# for 10 cycles

# Saving variables passed in from command line for later use
wait_time=$1
echo "Wait time in seconds: "$wait_time
increment=$2
echo "Increment in seconds: "$increment
cycle_count=$3
echo "Itteration cycles: "$cycle_count

# Turning network clock off
systemsetup -setusingnetworktime off

# getting on with it
read -p  "Hit any key when ready to begin"
init_time=$(systemsetup -gettime)
echo "Reference time will be set as: "$init_time

# converting current system time to seconds
d=$init_time
IFS=":"
set -- $d
hr=$2
min=$3
sec=$4

# strip leading white space from hr
hr=$(echo $hr | tr -d " ")

# echo "IFS time in seconds: $(((hr*3600)+(min*60)+sec))"
ref_time=$(((hr*3600)+(min*60)+sec))
# echo "\$ref_time set to: "$ref_time

#inserting while loop
COUNTER=0
while [  $COUNTER -lt $cycle_count ]; do
echo Cycle count: $COUNTER
echo "sleeping for $wait_time seconds"

# Don't want to sleep while testing
# sleep $wait_time

# Adding $increment to $ref_time
new_time=$((ref_time+increment))
# echo "old ref_time was: "$ref_time
ref_time=$new_time
# echo "new ref_time is: "$new_time

# Converting new_time to HH:MM:SS for set_time
secs=$new_time
# echo "\$secs set to: "$new_time

h=$(( secs / 3600 ))
m=$(( ( secs / 60 ) % 60 ))
s=$(( secs % 60 ))

echo "system time will be sets as: systemsetup -settime $h":"$m":"$s"
set_time=$h:$m:$s
echo $set_time
# systemsetup -settime $h":"$m":"$s"

let COUNTER=COUNTER+1
done

# Turning network clock on
systemsetup -setusingnetworktime on

---------- Post updated at 02:51 PM ---------- Previous update was at 02:32 PM ----------

Bugger!

I found the problem. line 25

Code:
IFS=":"

So I added
Code:
IFS=":"
set -- $d
hr=$2
min=$3
sec=$4

# Adding this as a wild hair, what if IFS above is causing problems?
IFS=";"

I have never used IFS, but I suspect that I should unset it, rather than setting to a semi-colon to solve the problem.

Thanks to everyone for all your help!

Last edited by xaiu; 03-13-2011 at 05:52 PM.. Reason: correcting typo
# 12  
Old 03-14-2011
Best bet is to store IFS in OLDIFS before you change it and then rest it back after:

Code:
OLD_IFS=$IFS
IFS=:
# your commands here
IFS=$OLD_IFS

Or set it explicitly to the shell default:
Code:
IFS=$' \t\n'

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