终止在詹金斯管道脚本中以背景运行的过程

发布于 2025-02-03 17:10:46 字数 780 浏览 2 评论 0原文

我有一个声明性的詹金斯管道,该管道使用脚本步骤在循环中执行两件事:

  1. 通过在后台运行bash脚本打开SSH隧道(在末尾添加&在末尾)
  2. 做其他一些事情在让SSH隧道打开问题时,
pipeline {
    stage('some stage') {
      steps {
        script {
          def hosts = ['host-0.service.consul', 'host-1.service.consul', 'host-2.service.consul']
          for (host in hosts){
            // kill previously opened tunnel here if it has been opened
            ???
            // run script in background
            sh "./scripts/myscript.sh &"
            // do some other stuff
            ...
          }
        }
      }
    }
  }
}

问题是,在每次迭代时,我必须通过杀死用sh的过程来关闭SSH隧道。但是,我不知道该过程的基础。

我该如何使用Groovy和/或Bash在Jenkins管道中进行此操作?

不确定这个问题是否与Bash或Jenkins/Groovy有关

I have a declarative Jenkins pipeline that uses a script step to do two things inside a loop:

  1. opening an SSH tunnel by running a bash script in the background (adding & at the end)
  2. do some other stuff while having the SSH tunnel open
pipeline {
    stage('some stage') {
      steps {
        script {
          def hosts = ['host-0.service.consul', 'host-1.service.consul', 'host-2.service.consul']
          for (host in hosts){
            // kill previously opened tunnel here if it has been opened
            ???
            // run script in background
            sh "./scripts/myscript.sh &"
            // do some other stuff
            ...
          }
        }
      }
    }
  }
}

The problem is that upon each iteration I have to close the SSH tunnel by killing the process spawned with sh "./scripts/myscript.sh &". However I don't know the PID of that process.

How can I do this inside a Jenkins pipeline using Groovy and/or Bash?

Not sure if this question is more related to bash or to Jenkins/Groovy

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文