Skip to content

Commit f547e81

Browse files
authored
Workaround for WSL2 hibernate clock drift
See microsoft/WSL#5324
1 parent 6a3d3d1 commit f547e81

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

wsl2-time-sync.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
cat << EOF > ~/bin/wsl2-timesync.sh
2+
#!/bin/bash
3+
threshold=5
4+
hwtime=`sudo hwclock --test | grep "^Hw clock time" | sed -re 's/.*= ([0-9]+) seconds since 1969/\1/g'`
5+
vmtime=`date +%s`
6+
timediff=$(($hwtime - vmtime))
7+
if [ "$timediff" -gt "${threshold}" ] || [ "$timediff" -lt "-${threshold}" ] ; then
8+
# If not running interactively, don't do anything
9+
case $- in
10+
*i*) ;;
11+
*) echo "Time difference ($timediff) drifted by more than +/- ${threshold}s. Syncing to hardware time.";;
12+
esac
13+
sudo hwclock -s
14+
fi
15+
EOF
16+
chmod u+x ~/bin/wsl2-timesync.sh
17+
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 commit comments

Comments
 (0)