Add desktop session detection to auto-boot-ollama-host script
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.
This commit is contained in:
@@ -66,4 +66,64 @@ function ssh_module.execute(command, user, host, port, identity_file)
|
||||
end
|
||||
end
|
||||
|
||||
-- Execute a remote command over SSH and return the output
|
||||
-- Signature: ssh.execute_with_output(command, user, host, port, identity_file)
|
||||
-- Returns: success, output, error_message
|
||||
function ssh_module.execute_with_output(command, user, host, port, identity_file)
|
||||
-- Basic validation and defaults
|
||||
user = tostring(user or "")
|
||||
host = tostring(host or "")
|
||||
port = tonumber(port or 22) or 22
|
||||
identity_file = tostring(identity_file or "")
|
||||
|
||||
-- Build base ssh command (run locally)
|
||||
local dest = (user ~= "" and (user .. "@" .. host) or host)
|
||||
local pieces = {
|
||||
"ssh",
|
||||
"-p", tostring(port),
|
||||
"-o", "BatchMode=yes",
|
||||
"-o", "ConnectTimeout=30",
|
||||
"-o", "ServerAliveInterval=5",
|
||||
"-o", "ServerAliveCountMax=1",
|
||||
"-o", "UserKnownHostsFile=/root/.ssh/known_hosts",
|
||||
"-o", "StrictHostKeyChecking=yes",
|
||||
}
|
||||
|
||||
if identity_file ~= "" then
|
||||
table.insert(pieces, "-i")
|
||||
table.insert(pieces, identity_file)
|
||||
end
|
||||
table.insert(pieces, dest)
|
||||
|
||||
-- Pass remote command as provided
|
||||
table.insert(pieces, "--")
|
||||
table.insert(pieces, command)
|
||||
|
||||
-- Join with spaces for io.popen
|
||||
local function join(args)
|
||||
return table.concat(args, " ")
|
||||
end
|
||||
|
||||
local full = join(pieces)
|
||||
utils.log("SSH exec (with output): " .. full)
|
||||
|
||||
-- Use io.popen to capture output
|
||||
local fh = io.popen(full, "r")
|
||||
if not fh then
|
||||
return false, "", "Failed to open SSH command"
|
||||
end
|
||||
|
||||
local output = fh:read("*a")
|
||||
local success, reason, code = fh:close()
|
||||
|
||||
if success then
|
||||
utils.log("SSH command completed successfully with output")
|
||||
return true, output, nil
|
||||
else
|
||||
local msg = string.format("SSH failed: reason=%s code=%s", tostring(reason), tostring(code))
|
||||
utils.log(msg)
|
||||
return false, output, msg
|
||||
end
|
||||
end
|
||||
|
||||
return ssh_module
|
||||
|
||||
Reference in New Issue
Block a user