12 Commits

Author SHA1 Message Date
pre-commit-ci[bot]
1c2f3b77ca [pre-commit.ci] pre-commit autoupdate
updates:
- [github.com/pre-commit/pre-commit-hooks: v4.6.0 → v6.0.0](https://github.com/pre-commit/pre-commit-hooks/compare/v4.6.0...v6.0.0)
- [github.com/scop/pre-commit-shfmt: v3.8.0-1 → v3.12.0-2](https://github.com/scop/pre-commit-shfmt/compare/v3.8.0-1...v3.12.0-2)
- [github.com/shellcheck-py/shellcheck-py: v0.10.0.1 → v0.11.0.1](https://github.com/shellcheck-py/shellcheck-py/compare/v0.10.0.1...v0.11.0.1)
2025-09-08 16:44:46 +00:00
Oskar Manhart
3ac8765a76 Merge pull request #574 from eylenburg/timesync
Add a function to sync time in Windows after Linux host is suspended (sleeping)
2025-09-08 09:11:46 +02:00
Oskar Manhart
88b6ee00a0 Merge pull request #712 from winapps-org/fix-setup-patch
Fix #711
2025-09-07 22:37:19 +02:00
Oskar Manhart
982de8a0f8 Merge branch 'main' into timesync 2025-09-04 22:37:15 +02:00
Oskar Manhart
43f86b9821 Merge branch 'main' into timesync 2025-09-04 22:37:01 +02:00
eylenburg
cdb5c3ef7f Update TimeSync.ps1
Signed-off-by: Oskar Manhart <52569953+oskardotglobal@users.noreply.github.com>
2025-09-04 22:36:48 +02:00
eylenburg
b53682be6c Update winapps
Signed-off-by: Oskar Manhart <52569953+oskardotglobal@users.noreply.github.com>
2025-09-04 22:36:48 +02:00
pre-commit-ci[bot]
fbb382e1e8 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci

Signed-off-by: Oskar Manhart <52569953+oskardotglobal@users.noreply.github.com>
2025-09-04 22:36:48 +02:00
eylenburg
a04f5db39a Create TimeSync.ps1
Signed-off-by: Oskar Manhart <52569953+oskardotglobal@users.noreply.github.com>
2025-09-04 22:36:48 +02:00
eylenburg
9708ca2825 Schedule timesync.ps1 in install.bat
Signed-off-by: Oskar Manhart <52569953+oskardotglobal@users.noreply.github.com>
2025-09-04 22:36:48 +02:00
eylenburg
3f909c5ec0 Update winapps
Signed-off-by: Oskar Manhart <52569953+oskardotglobal@users.noreply.github.com>
2025-09-04 22:36:48 +02:00
eylenburg
836f3703f6 add TimeSync() function to script
Signed-off-by: Oskar Manhart <52569953+oskardotglobal@users.noreply.github.com>
2025-09-04 22:36:48 +02:00
4 changed files with 99 additions and 3 deletions

View File

@@ -14,7 +14,7 @@ repos:
args: [ --whitespaces-count, "4" ]
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
rev: v6.0.0
hooks:
- id: check-added-large-files
- id: check-case-conflict
@@ -38,12 +38,12 @@ repos:
- id: trailing-whitespace
- repo: https://github.com/scop/pre-commit-shfmt
rev: v3.8.0-1
rev: v3.12.0-2
hooks:
- id: shfmt
args: ["-i", "4", "-ci", "-s"]
- repo: https://github.com/shellcheck-py/shellcheck-py
rev: v0.10.0.1
rev: v0.11.0.1
hooks:
- id: shellcheck

View File

@@ -27,6 +27,8 @@ readonly CONFIG_PATH="${HOME}/.config/winapps/winapps.conf"
readonly COMPOSE_PATH="${HOME}/.config/winapps/compose.yaml"
# shellcheck disable=SC2155 # Silence warnings regarding masking return values through simultaneous declaration and assignment.
readonly SCRIPT_DIR_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
readonly SLEEP_DETECT_PATH="${APPDATA_PATH}/last_activity"
readonly SLEEP_MARKER="${APPDATA_PATH}/sleep_marker"
# OTHER
readonly CONTAINER_NAME="WinApps" # FOR 'docker' AND 'podman' ONLY
@@ -736,6 +738,50 @@ function waCheckIdle() {
fi
}
# Name: 'waTimeSync'
# Role: Detect if system went to sleep by comparing uptime progression, then sync time in Windows VM
function waTimeSync() {
local CURRENT_TIME
local CURRENT_UPTIME
local STORED_TIME=0
local STORED_UPTIME=0
local EXPECTED_UPTIME=0
local UPTIME_DIFF=0
CURRENT_TIME=$(date +%s)
CURRENT_UPTIME=$(awk '{print int($1)}' /proc/uptime)
# Read stored values if file exists
if [ -f "$SLEEP_DETECT_PATH" ]; then
STORED_TIME=$(head -n1 "$SLEEP_DETECT_PATH" 2>/dev/null || echo 0)
STORED_UPTIME=$(tail -n1 "$SLEEP_DETECT_PATH" 2>/dev/null || echo 0)
fi
if [ "$STORED_TIME" -gt 0 ] && [ "$STORED_UPTIME" -gt 0 ]; then
# Calculate what uptime should be now
EXPECTED_UPTIME=$((STORED_UPTIME + CURRENT_TIME - STORED_TIME))
UPTIME_DIFF=$((EXPECTED_UPTIME - CURRENT_UPTIME))
dprint "UPTIME_DIFF: ${UPTIME_DIFF} seconds"
# If uptime is significantly less than expected, system likely slept
if [[ "$UPTIME_DIFF" -gt 30 && ! -f "$SLEEP_MARKER" ]]; then
dprint "DETECTED SLEEP/WAKE CYCLE (uptime gap: ${UPTIME_DIFF}s). CREATING SLEEP MARKER TO SYNC WINDOWS TIME."
# Create sleep marker which will be monitored by Windows VM to trigger time sync
touch "$SLEEP_MARKER"
dprint "CREATED SLEEP MARKER"
fi
fi
# Store current values
{
echo "$CURRENT_TIME"
echo "$CURRENT_UPTIME"
} > "$SLEEP_DETECT_PATH"
}
### MAIN LOGIC ###
#set -x # Enable for debugging.
dprint "START"
@@ -765,6 +811,7 @@ else
fi
waCheckPortOpen
waTimeSync
waRunCommand "$@"
if [[ "$AUTOPAUSE" == "on" ]]; then

32
oem/TimeSync.ps1 Normal file
View File

@@ -0,0 +1,32 @@
# Script to monitor if there is a sleep_marker created by WinApps (indicating the Linux host was suspended) in order to trigger a time sync as the time in the Windows VM will otherwise drift while Linux is suspended.
# Define the path to monitor. Make sure this matches the location for the sleep_marker in the Winapps script (need to match the APPDATA path).
$filePath = "\\tsclient\home\.local\share\winapps\sleep_marker"
$networkPath = "\\tsclient\home"
# Function to check and handle file
function Monitor-File {
while ($true) {
# Check if network location is available
try {
$null = Test-Path -Path $networkPath -ErrorAction Stop
# Check if file exists
if (Test-Path -Path $filePath) {
# Run time resync silently
w32tm /resync /quiet
# Remove the file
Remove-Item -Path $filePath -Force
}
}
catch {
# Network location not available, continue monitoring silently
}
# Wait 5 minutes before next check
Start-Sleep -Seconds 3000
}
}
# Start monitoring silently
Monitor-File

View File

@@ -77,3 +77,20 @@ if %ERRORLEVEL% neq 0 (
echo [ERROR] Failed to create scheduled task "%taskname%".
)
)
REM Create time sync task to be run by the user at login
copy %~dp0\TimeSync.ps1 %windir%
set "taskname2=TimeSync"
set "command2=powershell.exe -WindowStyle Hidden -ExecutionPolicy Bypass -File \"%windir%\TimeSync.ps1\""
schtasks /query /tn "%taskname2%" >nul
if %ERRORLEVEL% equ 0 (
echo %DATE% %TIME% Task "%taskname2%" already exists, skipping creation.
) else (
schtasks /create /tn "%taskname2%" /tr "%command2%" /sc onlogon /rl HIGHEST /f
if %ERRORLEVEL% equ 0 (
echo %DATE% %TIME% Scheduled task "%taskname2%" created successfully.
) else (
echo %DATE% %TIME% Failed to create scheduled task %taskname2%.
)
)