简体   繁体   中英

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:

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. 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 .
- Then add the wait command after the kill:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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