Browse Source
Implement a new module for checking if the user is logged into a Windows desktop session. The script now skips Ollama startup and shutdown if the user is currently logged in, preventing interruptions. Update README to reflect new features and module structure.main
4 changed files with 158 additions and 1 deletions
@ -0,0 +1,45 @@ |
|||||
|
-- Windows desktop session check module for auto-boot-ollama-host |
||||
|
-- Checks if a specific user is currently logged into a Windows desktop session |
||||
|
|
||||
|
local utils = require("utils") |
||||
|
local ssh = require("ssh") |
||||
|
|
||||
|
local session_check = {} |
||||
|
|
||||
|
-- Check if user is currently logged into a Windows desktop session using quser |
||||
|
-- Returns true if user is logged in, false otherwise |
||||
|
function session_check.is_user_logged_in(config) |
||||
|
utils.log(("Checking if user '%s' is logged into desktop session using quser..."):format(config.SSH_USER)) |
||||
|
|
||||
|
-- Use quser command to check for active sessions |
||||
|
-- This works regardless of Windows language (looks for console/rdp sessions) |
||||
|
local command = "quser 2>nul | findstr /i \"console rdp-tcp#\" >nul && echo 1 || echo 0" |
||||
|
|
||||
|
local ok, output, err = ssh.execute_with_output(command, config.SSH_USER, config.OLLAMA_HOST, config.SSH_PORT, config.SSH_IDENTITY_FILE) |
||||
|
|
||||
|
if not ok then |
||||
|
utils.log(("Failed to execute quser session check command: %s"):format(tostring(err))) |
||||
|
return false |
||||
|
end |
||||
|
|
||||
|
if output and output ~= "" then |
||||
|
utils.log("Quser session check output received: " .. output) |
||||
|
|
||||
|
-- Parse the result (remove any whitespace/newlines) |
||||
|
local clean_output = output:gsub("%s+", "") |
||||
|
local result = tonumber(clean_output) |
||||
|
|
||||
|
if result == 1 then |
||||
|
utils.log(("User '%s' has an active desktop session"):format(config.SSH_USER)) |
||||
|
return true |
||||
|
else |
||||
|
utils.log(("User '%s' has no active desktop sessions"):format(config.SSH_USER)) |
||||
|
return false |
||||
|
end |
||||
|
else |
||||
|
utils.log("No output received from quser session check command") |
||||
|
return false |
||||
|
end |
||||
|
end |
||||
|
|
||||
|
return session_check |
||||
Loading…
Reference in new issue