File tree Expand file tree Collapse file tree 1 file changed +17
-0
lines changed Expand file tree Collapse file tree 1 file changed +17
-0
lines changed Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments