@@ -179,24 +179,43 @@ Use-ProxyIfNeeded -Script {
179179$taskStartup = " drpyS_PM2_Startup"
180180$taskUpdate = " drpyS_Update"
181181
182- if (-not (Get-ScheduledTask - TaskName $taskStartup - ErrorAction SilentlyContinue)) {
183- $action = New-ScheduledTaskAction - Execute " powershell.exe" `
184- - Argument " -NoProfile -WindowStyle Hidden -Command pm2 resurrect"
185- $trigger = New-ScheduledTaskTrigger - AtStartup - RandomDelay (New-TimeSpan - Seconds 30 )
186- $setting = New-ScheduledTaskSettingsSet - AllowStartIfOnBatteries - DontStopIfGoingOnBatteries - StartWhenAvailable
187- Register-ScheduledTask - TaskName $taskStartup - Action $action - Trigger $trigger `
188- - Settings $setting - User " SYSTEM" - RunLevel Highest - Force | Out-Null
189- Write-Host " 已创建开机自启任务:$taskStartup " - ForegroundColor Yellow
190- }
182+ # 先拿 pm2 的绝对路径;拿不到就提示并跳过
183+ $pm2 = (Get-Command pm2.cmd - ErrorAction SilentlyContinue).Source
184+ if (-not $pm2 ) {
185+ Write-Warning " 找不到 pm2.cmd,跳过计划任务注册"
186+ } else {
187+ # 强制删除旧任务(若存在)
188+ $taskStartup , $taskUpdate | ForEach-Object {
189+ if (Get-ScheduledTask - TaskName $_ - ErrorAction SilentlyContinue) {
190+ Unregister-ScheduledTask - TaskName $_ - Confirm:$false
191+ }
192+ }
191193
192- if (-not (Get-ScheduledTask - TaskName $taskUpdate - ErrorAction SilentlyContinue)) {
193- $action = New-ScheduledTaskAction - Execute " powershell.exe" `
194- - Argument " -NoProfile -WindowStyle Hidden -Command `" & { cd '$projectPath '; git fetch origin; if (git status -uno | Select-String 'Your branch is behind') { git reset --hard origin/main; yarn --prod --silent; if (git diff HEAD^ HEAD --name-only | Select-String 'spider/py/base/requirements.txt') { python -m venv .venv; & .\.venv\Scripts\Activate.ps1; pip install -r spider\py\base\requirements.txt -q } pm2 restart drpyS } }`" "
195- $trigger = New-ScheduledTaskTrigger - Once - At (Get-Date ) - RepetitionInterval (New-TimeSpan - Hours 24 )
196- $setting = New-ScheduledTaskSettingsSet - AllowStartIfOnBatteries - DontStopIfGoingOnBatteries - StartWhenAvailable
197- Register-ScheduledTask - TaskName $taskUpdate - Action $action - Trigger $trigger `
198- - Settings $setting - User " SYSTEM" - RunLevel Highest - Force | Out-Null
199- Write-Host " 已创建每 24 小时更新任务:$taskUpdate " - ForegroundColor Yellow
194+ # 通用设置
195+ $commonSettings = New-ScheduledTaskSettingsSet `
196+ - AllowStartIfOnBatteries - DontStopIfGoingOnBatteries - StartWhenAvailable
197+
198+ # 1) 开机自启
199+ $action = New-ScheduledTaskAction `
200+ - Execute " cmd.exe" `
201+ - Argument " /c `" $pm2 `" resurrect"
202+ $trigger = New-ScheduledTaskTrigger `
203+ - AtStartup - RandomDelay (New-TimeSpan - Seconds 30 )
204+ Register-ScheduledTask - TaskName $taskStartup `
205+ - Action $action - Trigger $trigger - Settings $commonSettings `
206+ - User " SYSTEM" - RunLevel Highest - Force | Out-Null
207+ Write-Host " 已创建/更新开机自启任务:$taskStartup " - ForegroundColor Yellow
208+
209+ # 2) 每 24h 更新
210+ $action = New-ScheduledTaskAction `
211+ - Execute " powershell.exe" `
212+ - Argument " -NoProfile -ExecutionPolicy Bypass -WindowStyle Hidden -Command `" & { Set-Location '$projectPath '; git fetch origin; if (git status -uno | Select-String 'Your branch is behind') { git reset --hard origin/main; yarn --prod --silent; if (git diff HEAD^ HEAD --name-only | Select-String 'spider/py/base/requirements.txt') { python -m venv .venv; & .\.venv\Scripts\Activate.ps1; pip install -r spider\py\base\requirements.txt -q } & '$pm2 ' restart drpyS } }`" "
213+ $trigger = New-ScheduledTaskTrigger `
214+ - Once - At (Get-Date ) - RepetitionInterval (New-TimeSpan - Hours 24 )
215+ Register-ScheduledTask - TaskName $taskUpdate `
216+ - Action $action - Trigger $trigger - Settings $commonSettings `
217+ - User " SYSTEM" - RunLevel Highest - Force | Out-Null
218+ Write-Host " 已创建/更新每 24 小时更新任务:$taskUpdate " - ForegroundColor Yellow
200219}
201220
202221# ---------- 完成 ----------
0 commit comments