Skip to content

Commit

Permalink
Workaround for WSL2 hibernate clock drift
Browse files Browse the repository at this point in the history
  • Loading branch information
muchtall authored Sep 17, 2020
1 parent 6a3d3d1 commit f547e81
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions wsl2-time-sync.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
cat << EOF > ~/bin/wsl2-timesync.sh
#!/bin/bash
threshold=5
hwtime=`sudo hwclock --test | grep "^Hw clock time" | sed -re 's/.*= ([0-9]+) seconds since 1969/\1/g'`
vmtime=`date +%s`
timediff=$(($hwtime - vmtime))
if [ "$timediff" -gt "${threshold}" ] || [ "$timediff" -lt "-${threshold}" ] ; then
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) echo "Time difference ($timediff) drifted by more than +/- ${threshold}s. Syncing to hardware time.";;
esac
sudo hwclock -s
fi
EOF
chmod u+x ~/bin/wsl2-timesync.sh
if [ "`sudo grep wsl2-timesync.sh /etc/crontab | grep -Pv "^\s*#" | wc -l`" -eq "0" ] ; then sudo bash -c "echo -e \"* * * * *\troot\t`realpath ~/bin/wsl2-timesync.sh`\" >> /etc/crontab" ; fi

0 comments on commit f547e81

Please sign in to comment.