Hide Windows scheduler bot execution
This commit is contained in:
@@ -17,6 +17,14 @@ from app.main import load_env
|
||||
load_env()
|
||||
|
||||
|
||||
def _bot_python() -> str:
|
||||
if os.name == "nt":
|
||||
pythonw = Path(sys.executable).with_name("pythonw.exe")
|
||||
if pythonw.exists():
|
||||
return str(pythonw)
|
||||
return sys.executable
|
||||
|
||||
|
||||
def _is_process_alive(pid: int) -> bool:
|
||||
result = subprocess.run(
|
||||
["tasklist", "/FI", f"PID eq {pid}", "/NH"],
|
||||
@@ -69,7 +77,7 @@ def _restart_bot() -> int:
|
||||
log_path = PROJECT / "logs" / "bot_stderr.log"
|
||||
with open(log_path, "a", encoding="utf-8") as log:
|
||||
proc = subprocess.Popen(
|
||||
[sys.executable, "-u", "app/main.py"],
|
||||
[_bot_python(), "-u", "app/main.py"],
|
||||
cwd=PROJECT,
|
||||
creationflags=creationflags,
|
||||
stdout=log,
|
||||
|
||||
@@ -25,7 +25,7 @@ function Register-StockTask {
|
||||
$Trigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek $Weekdays -At $Time
|
||||
$Action = New-ScheduledTaskAction `
|
||||
-Execute "powershell.exe" `
|
||||
-Argument "-NonInteractive -ExecutionPolicy Bypass -File `"$Project\scripts\$Script`"" `
|
||||
-Argument "-NoProfile -WindowStyle Hidden -NonInteractive -ExecutionPolicy Bypass -File `"$Project\scripts\$Script`"" `
|
||||
-WorkingDirectory $Project
|
||||
$Settings = New-ScheduledTaskSettingsSet `
|
||||
-ExecutionTimeLimit (New-TimeSpan -Minutes $LimitMinutes) `
|
||||
@@ -34,6 +34,7 @@ function Register-StockTask {
|
||||
-DontStopIfGoingOnBatteries `
|
||||
-RunOnlyIfNetworkAvailable:$false
|
||||
$Settings.DisallowStartIfOnBatteries = $false
|
||||
$Settings.Hidden = $true
|
||||
|
||||
Register-ScheduledTask `
|
||||
-TaskName $Name `
|
||||
@@ -49,7 +50,7 @@ function Register-StockTask {
|
||||
|
||||
function Register-WatchdogTask {
|
||||
$ScriptPath = Join-Path $Project "scripts\run_watchdog.ps1"
|
||||
$Command = 'schtasks /Create /TN "\StockBot\StockBot_Watchdog" /TR "\"powershell.exe\" -NonInteractive -ExecutionPolicy Bypass -File \"' + $ScriptPath + '\"" /SC WEEKLY /D MON,TUE,WED,THU,FRI /ST 09:00 /RI 5 /DU 06:05 /F'
|
||||
$Command = 'schtasks /Create /TN "\StockBot\StockBot_Watchdog" /TR "\"powershell.exe\" -NoProfile -WindowStyle Hidden -NonInteractive -ExecutionPolicy Bypass -File \"' + $ScriptPath + '\"" /SC WEEKLY /D MON,TUE,WED,THU,FRI /ST 09:00 /RI 5 /DU 06:05 /F'
|
||||
cmd.exe /c $Command | Out-Null
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
throw "StockBot_Watchdog registration failed"
|
||||
@@ -60,6 +61,7 @@ function Register-WatchdogTask {
|
||||
$Task.Settings.WakeToRun = $true
|
||||
$Task.Settings.DisallowStartIfOnBatteries = $false
|
||||
$Task.Settings.StopIfGoingOnBatteries = $false
|
||||
$Task.Settings.Hidden = $true
|
||||
Set-ScheduledTask -TaskName "StockBot_Watchdog" -TaskPath $TaskPath -Settings $Task.Settings | Out-Null
|
||||
|
||||
Write-Host "[OK] StockBot_Watchdog registered weekdays at 09:00-15:05 every 5 minutes" -ForegroundColor Green
|
||||
|
||||
@@ -11,6 +11,14 @@ PID_FILE = PROJECT / "logs" / "bot.pid"
|
||||
LOG_FILE = PROJECT / "logs" / "bot_stderr.log"
|
||||
|
||||
|
||||
def _bot_python() -> str:
|
||||
if os.name == "nt":
|
||||
pythonw = Path(sys.executable).with_name("pythonw.exe")
|
||||
if pythonw.exists():
|
||||
return str(pythonw)
|
||||
return sys.executable
|
||||
|
||||
|
||||
def _taskkill(pid: int) -> None:
|
||||
subprocess.run(["taskkill", "/PID", str(pid), "/F"], capture_output=True, text=True)
|
||||
|
||||
@@ -69,7 +77,7 @@ def _start_bot() -> int:
|
||||
|
||||
with open(LOG_FILE, "a", encoding="utf-8") as log:
|
||||
proc = subprocess.Popen(
|
||||
[sys.executable, "-u", "app/main.py"],
|
||||
[_bot_python(), "-u", "app/main.py"],
|
||||
cwd=PROJECT,
|
||||
creationflags=creationflags,
|
||||
stdout=log,
|
||||
|
||||
Reference in New Issue
Block a user