Gitlab Runner陷入后台进程卡住。

huangapple go评论70阅读模式
英文:

gitlab runner gets stuck with background process

问题

在GitLab CI/CD中,我想要启动一个后台进程(在Ubuntu中使用Bash)。
GitLab Runner的类型是shell。
我正在使用Ubuntu服务器,并手动安装了GitLab Runner。
问题是,如果我启动任何后台进程,GitLab Runner也会等待它。
这当然不是我的意图,因为它应该启动服务器。
我还尝试了一些非常简单的方法:

#!/bin/bash
echo "Starting background process."
sleep 900 &
echo "Finished."

"Finished." 会立即出现在日志中,如预期那样。
但是GitLab Runner 没有结束,它会等待后台的 sleep 命令(超时运行)。没有后台进程时,它会正常结束。

如何在启动后台进程的同时正常结束GitLab Runner?

英文:

In Gitalb CI/CD, I want to start a server as a background process (Bash in Ubuntu).
Gitlab-runner is type shell.
I am using Ubuntu server and manually installed gitlab-runner.
The problem is, if I start any background process, gitlab-runner is waiting for it as well.
That is of course not the intention, since it shall start the server.
I tried also with something very simple:

#!/bin/bash
echo "Starting background process."
sleep 900 &
echo "Finished."

The "Finished." appears immediately in the log, as expected.
But gitlab-runner does not finish, waiting for the background sleep command as well (running into timeout). Without background process, it finishes normally.

How can I start a background process and yet finish gitlab-runner normally ?

答案1

得分: 1

我自己解决了。原因似乎是后台进程,即使解绑了,也会写入STDOUT,显然这会阻止作业完成。解决方案是使用nohup,并将任何输出重定向到/dev/null,执行带有服务器启动命令的脚本:

nohup bash ./run.sh >/dev/null 2>&1 &

现在gitlab-runner可以正常完成。然而,这仍然是gitlab-runner的奇怪行为。

英文:

I solved it myself. The reason seems to be that the background process, even if disowned, writes to STDOUT and obviously preventing with that to finish the job. The solution was to use nohup and redirect any output to /dev/null executing the script with server start command:

nohup bash ./run.sh >/dev/null 2>&1 &

Now the gitlab-runner finishes normally.
Yet, this is still strange behavior from gitlab-runner.

huangapple
  • 本文由 发表于 2023年6月26日 13:27:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76553732.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定