Reputation: 3422
I have a Powerbook with a dead laptop battery... Everytime I disconnect it from it's power source the date gets set back to the year 1969. This leads to some issues with network authentication, keychain access, etc. I wrote a launch daemon that executes this script in order to set the date to something more appropriate at boot, which works fantastically.
#!/bin/bash
date 0101122014
Now I only want it to only run if the year is 1969, that way if the time is correct it won't get set back at all.
so something like...
#!/bin/bash
YEAR="date +%Y"
if [ $YEAR = 1969 ] ;
then date 010112002014
else exit 0
fi
I know the syntax is totally off, it's just to give an idea of what I want to do. Thanks for any help in advance.
Upvotes: 0
Views: 106
Reputation: 3422
So I got everything setup with your guys help & it works great. I used one script and a launch daemon to set, save & set time via ntp. Here they are :) There are a million ways to skin a cat this is how I skinned mine.
Heres the script:
#!/bin/bash
#Load variables from .conf file
. /usr/share/fixes/time/time.conf
#Variables
DATE=$(date +%m%d%H%M%Y)
#Check year & set time from variable in .conf file
[ $(date +%Y) = 1969 ] && date $TIME
#Update time via ntp server
ntpdate -u $(systemsetup -getnetworktimeserver|awk '{print $4}')
#Save time to variable in .conf file
sed -i -e "s/^TIME=.*/TIME=$DATE/" /usr/share/fixes/time/time.conf
exit 0
& launch daemon .plist for OSX which runs script every 3 minutes:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.fix.settime</string>
<key>ProgramArguments</key>
<array>
<string>./usr/share/fixes/time/time-fix.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>StartInterval</key>
<integer>180</integer>
</dict>
Thank you to all those that helped, I really appreciate it and feel I have learned something from all of you once again.
Upvotes: 0
Reputation: 63972
Another approach
#!/bin/bash
monfile="/tmp/.com.example.touchfile"
#if have OK (current) date set the file modification time
have_current_date() {
date > "$monfile"
}
#set the date from the last file modification time
have_wrong_date() {
[ -f "$monfile" ] || touch -t 201401010101 "$monfile"
eval $(stat -s "$monfile") #get the file modification time to shell variable
date -f '%s' $st_mtime #set the date to "last" file modification time
}
#main program
YEAR=$(date +%Y)
case "$YEAR" in
2014) have_current_date ;;
*) have_wrong_date ;;
esac
what is doing
$monfile
- set the file modification time to current timeUpvotes: 2
Reputation: 5424
simple one using if
:
if [ $(date +%Y) = 1969 ]
then date 010112002014
else exit 0
fi
Upvotes: 1
Reputation: 754820
Use command substitution:
YEAR=$(date +%Y)
Or even:
[ $(date +%Y) = 1969 ] && date 010112002014
Or, using an if
:
if [ $(date +%Y) = 1969 ]
then date 010112002014
fi
Upvotes: 1
Reputation: 328770
Execute this command to set the date:
sudo ntpdate -u time.apple.com
ntpdate
will query the specified time server for the current date/time and then update the local time.
Note: The error in your script is YEAR="date +%Y"
: that just assigns the string date +%Y
to YEAR
. But you want to execute date +%Y
and assign the result:
YEAR=$(date +%Y)
Related:
Upvotes: 3