Last active
August 29, 2015 14:10
-
-
Save luisfdez/e6bd585975d52a02591d to your computer and use it in GitHub Desktop.
Actuator scripts to restart wsgate when it's stuck (PowerShell functionality + Python Windows Service wrapper)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Create EventLog for messages | |
# | |
If(-Not [System.Diagnostics.EventLog]::SourceExists('RdpConsoleActuator')) | |
{ | |
New-EventLog -LogName Application -Source "RdpConsoleActuator" | |
} | |
Write-EventLog -LogName Application -Source "RdpConsoleActuator" -EntryType Information -EventID 1 -Message "[$(Get-Date -Format g)] - RDP Console Actuator starting..." | |
# | |
# Test the availability of the RDP port | |
# | |
While($true) | |
{ | |
Try | |
{ | |
$tcpClient = New-Object System.Net.Sockets.TcpClient | |
$tcpClient.Connect("localhost", 7080) | |
} | |
Catch | |
{ | |
Write-EventLog -LogName Application -Source "RdpConsoleActuator" -EntryType Error -EventID 100 -Message "[$(Get-Date -Format g)] - Wsgate process stuck. Restarting the FreeRDP HTML5 Proxy." | |
Stop-Process -Name wsgate -Force | |
Start-Service wsgate | |
} | |
Finally | |
{ | |
$tcpClient.Dispose() | |
} | |
Start-Sleep -Seconds 30 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import psutil | |
import servicemanager | |
import subprocess | |
import sys | |
import win32service | |
import win32serviceutil | |
def kill_proc_tree(pid, including_parent=True): | |
parent = psutil.Process(pid) | |
for child in parent.get_children(recursive=True): | |
servicemanager.LogInfoMsg("RdpActuator: Stopping (Killing child: %s)" % child) | |
child.kill() | |
if including_parent: | |
servicemanager.LogInfoMsg("RdpActuator: Stopping (Killing parent: %s)" % parent) | |
parent.kill() | |
class RdpActuator(win32serviceutil.ServiceFramework): | |
_svc_name_ = "rdp-console-actuator" | |
_svc_display_name_ = "rdp-console-actuator" | |
_svc_description_ = "" | |
def __init__(self,args): | |
win32serviceutil.ServiceFramework.__init__(self,args) | |
self.p = None | |
def SvcDoRun(self): | |
servicemanager.LogInfoMsg('RdpActuator: Starting') | |
self.p = subprocess.Popen([r'C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe', | |
'-ExecutionPolicy', | |
'Unrestricted', | |
'-File', | |
r'C:/Program Files (x86)/Cloudbase Solutions/FreeRDP-WebConnect/Binaries/RdpActuator.ps1']) | |
self.p.wait() | |
servicemanager.LogInfoMsg("RdpActuator: Stopped") | |
def SvcStop(self): | |
servicemanager.LogInfoMsg("RdpActuator: Recieved stop signal") | |
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) | |
kill_proc_tree(self.p.pid) | |
if __name__ == '__main__': | |
win32serviceutil.HandleCommandLine(RdpActuator) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment