简体   繁体   English

杀死python后台进程gitlab-ci

[英]Kill python background process gitlab-ci

I'm trying to kill a python job started in background in an Alpine docker in a gitlab-ci:我正在尝试在 gitlab-ci 中的 Alpine docker 中终止在后台启动的 python 作业:

Python Python

import asyncio

def main():
    loop = asyncio.get_event_loop()
    try:
        loop.run_forever()
    except KeyboardInterrupt:
        print('Stopping')


if __name__ == '__main__':
    main()

Here are the commands that are ran.以下是运行的命令。

$ COVERAGE_FILE=.coverage.test coverage run test.py &
$ TEST_PID=$!
$ echo "${TEST_PID}"
26
$ kill -SIGINT ${TEST_PID}
$ jobs -l
[1]+  26 Running                 
$ kill -9 ${TEST_PID}
$ jobs -l
[1]+  26 Running                 

I can never see the .coverage.test as the job never finishes.我永远看不到.coverage.test因为工作永远不会完成。 However it seems to work fine when I run the commands locally.但是,当我在本地运行命令时,它似乎工作正常。

Finally found the solution:终于找到了解决办法:
- First, add signal.signal(signal.SIGINT, quit_gracefully) as mentioned here . - 首先,添加signal.signal(signal.SIGINT, quit_gracefully)这里提到的。
- Then add the wait command after the kill: - 然后在kill后添加wait命令:

COVERAGE_FILE=.coverage.test coverage run test.py &
TEST_PID=$!
kill -SIGINT ${TEST_PID}
wait

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM