# keyfarm cron runner: one-shot hunt with duplicate guard # schtasks: keyfarm-ds-hunt (30 min), keyfarm-uni-hunt (60 min) param([Parameter(Mandatory)][ValidateSet('ds','uni')][string]$Hunt) $kh = "C:\Users\User\Desktop\keyhunter" $py = "C:\Users\User\AppData\Local\Programs\Python\Python311\python.exe" switch ($Hunt) { 'ds' { $script = "$kh\dashscope_hunt.py"; $log = "$kh\data\dashscope_hunt.log"; $so = "$kh\data\cron_stdout_ds.log"; $se = "$kh\data\cron_stderr_ds.log" } 'uni' { $script = "$kh\universal_hunt.py"; $log = "$kh\data\universal_hunt.log"; $so = "$kh\data\cron_stdout_uni.log"; $se = "$kh\data\cron_stderr_uni.log" } } # guard: skip if another instance of this hunt is already running $leaf = Split-Path $script -Leaf $running = Get-CimInstance Win32_Process -Filter "Name='python.exe'" | Where-Object { $_.CommandLine -match $leaf } if ($running) { Add-Content -Path $log -Value ("[{0}] cron: {1} already running (pid {2}), skip" -f (Get-Date -Format 'yyyy-MM-dd HH:mm:ss'), $leaf, ($running.ProcessId -join ',')) exit 0 } Add-Content -Path $log -Value ("[{0}] cron: launching {1}" -f (Get-Date -Format 'yyyy-MM-dd HH:mm:ss'), $leaf) # python writes its own log internally; stdout goes to separate files # (a shared redirect would hold the log handle and break python's log()) $p = Start-Process -FilePath $py -ArgumentList "`"$script`"" -WindowStyle Hidden -PassThru ` -RedirectStandardOutput $so -RedirectStandardError $se $p.PriorityClass = "BelowNormal" Wait-Process -Id $p.Id exit 0