Skip to content

Instantly share code, notes, and snippets.

@rigred
Last active January 22, 2021 10:35
Show Gist options
  • Save rigred/2f029f237ca73f910d0ded5bbc990a0b to your computer and use it in GitHub Desktop.
Save rigred/2f029f237ca73f910d0ded5bbc990a0b to your computer and use it in GitHub Desktop.
Script to set date/time of macbook without a battery

Copy the launch daemon file named com.startup.script.plist in /Library/LaunchDaemons. Then set the permissions for it

sudo chmod +x /Library/LaunchDaemons/com.startup.script.plist
sudo chown root:wheel /Library/LaunchDaemons/com.startup.script.plist

Next copy the shell script updatetime.sh in /usr/local/bin/. Make it executable with sudo chmod +x /usr/local/bin/updatetime.sh and owned by root:wheel with sudo chown root:wheel /usr/local/bin/updatetime.sh.

Enable and launch the Daemon with:

sudo launchctl load -w /Library/LaunchDaemons/com.startup.script.plist
<?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.startup.script</string>
<key>LaunchOnlyOnce</key>
<true/>
<key>ProgramArguments</key>
<array>
<string>sh</string>
<string>-c</string>
<string>/usr/local/bin/updatetime.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
#!/bin/bash
# Insane bash script that sets date and time of macbook via ntpdate when the battery has been removed.
# Don't try to use `ntpd -q` instead of `ntpdate <server>` it's fuuuuucked.
FILE=lastdate.txt
LOCK=shutdown.lock
function ifup {
((count = 100)) # Maximum number to try.
while [[ $count -ne 0 ]] ; do
ping -c 1 8.8.8.8 # Try once.
rc=$?
if [[ $rc -eq 0 ]] ; then
((count = 1)) # If okay, flag to exit loop.
fi
((count = count - 1)) # So we don't go forever.
done
if [[ $rc -eq 0 ]] ; then # Make final determination.
echo "The internet is back up. Setting date and time."
ntpdate time.euro.apple.com
else
echo "Internet connection unavailable. Time may be incorrect."
osascript -e 'display notification "Please connect to internet and restart to update time." with title "Time updater"'
fi
}
function writedate {
datevars="lastdate=$(date +%m%d%H%M%Y)";
echo "$datevars" > "$FILE"
touch $LOCK
}
function filedate {
if test -f "$FILE"; then
echo "Date data $FILE exists."
source $FILE
date "$lastdate"
ifup
writedate
else
echo "Date data $FILE not found. Setting January 2021 and forcing network time update."
date 012210062021
ifup
writedate
fi
}
function update {
if test -f "$LOCK"; then
echo "Lock file exists"
ifup
writedate
echo "Updated time to last known time"
else
filedate
fi
}
function shutdown {
echo "$datevars"
rm $LOCK
writedate
}
trap shutdown SIGTERM
trap shutdown SIGKILL
if [ -f "$LOCK" ] ; then
osascript -e 'display notification "Last shutdown was abrupt, time may be wrong!" with title "Time updater"'
fi
osascript -e 'display notification "Please make sure internet is connected, else time may be wrong." with title "Time updater"'
while true; do update & sleep 60; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment