持续运行python脚本,该脚本与Azure Pipelines和GitHub一起部署

时间:2020-08-19 14:27:48

标签: python azure azure-devops azure-pipelines azure-pipelines-yaml

我已通过Azure Pipelines将forecasts.py文件从GitHub传输到我的虚拟机。如果我使用python3 forecasts.py &从虚拟机终端启动脚本,则一切运行顺利,脚本仍在后台运行。由于某种原因,如果我尝试类似地启动该脚本,则会从Azure管道获得以下消息:

The STDIO streams did not close within 10 seconds of the exit event from process '/bin/bash'. This may indicate a child process inherited the STDIO streams and has not yet exited.

可以找到完整的调试日志here

forecasts.py的核心内容如下:

import schedule
import time

def job():
    print("I'm working...")

schedule.every().minute.at(":00").do(job)

while True:
    schedule.run_pending()
    time.sleep(5)

此脚本应每分钟打印一次“我正在工作...”。我应该以其他方式启动脚本吗?

编辑

azure-pipelines.yml可能有助于解决此问题:

variables:
- name: system.debug
  value: true 

jobs:
- deployment: fmi_forecasts_deployment
  displayName: fmi_forecasts
  environment:
    name: AnalyticsServices
    resourceType: VirtualMachine
  strategy:
      rolling:
        maxParallel: 2  #for percentages, mention as x%
        preDeploy:
          steps:
          - download: current
          - script: echo initialize, cleanup, backup, install certs
        deploy:
          steps:
          - checkout: self
          - script: sudo apt install python3-pip
            displayName: 'Update pip'
          - script: python3 -m pip install -r requirements.txt
            displayName: 'Install requirements.txt modules'
          - script: rsync -a $(Build.SourcesDirectory) /home/ubuntu/$(Build.Repository.Name)/
            displayName: 'Sync files to $(Build.Repository.Name)'
          - task: Bash@3
            inputs:
              targetType: 'inline'
              script: python3 /home/ubuntu/$(Build.Repository.Name)/s/forecasts.py &
            displayName: 'Start the script'
        routeTraffic:
          steps:
          - script: echo routing traffic
        postRouteTraffic:
          steps:
          - script: echo health check post-route traffic
        on:
          failure:
            steps:
            - script: echo Restore from backup! This is on failure
          success:
            steps:
            - script: echo Notify! This is on success    

编辑

我编辑了forecasts.py文件,每5秒钟打印一次“ Sleeping ...”。当我使用nohup python -u /home/ubuntu/$(Build.Repository.Name)/s/forecasts.py &执行该操作时,我将收到以下日志。因此,该脚本有效,但是当我查看VM中正在运行的进程时,没有任何python进程在运行。脚本死了,我认为当管道结束时。

1 个答案:

答案 0 :(得分:0)

## [debug]该任务被标记为“完成”,但是该过程在5秒钟后仍未关闭。将任务视为已完成。

根据调试日志,这应该更像是一条提示消息,指示某些进程仍在运行并且尚未清除,而不是一条未写入标准错误流且导致任务失败的错误消息

如果您希望该脚本在任务完成后继续在后台运行。您可以尝试使用start-process命令启动脚本。这将确保任务完成后启动的作业继续运行。但是,构建完成后,该作业将关闭。

Start-Process powershell.exe -ArgumentList '-file xxx\forecasts.py'

有关详细信息,请参阅此ticket中的解决方法。

相关问题