2

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.

Orelus
  • 963
  • 1
  • 13
  • 23
  • Possible duplicate of [How to send a SIGINT to Python from a bash script?](https://stackoverflow.com/questions/974189/how-to-send-a-sigint-to-python-from-a-bash-script) – Arne Mar 15 '19 at 07:16
  • I saw and tried the above; it did not work for me – Orelus Mar 15 '19 at 07:26

1 Answers1

2

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
Orelus
  • 963
  • 1
  • 13
  • 23