0

I am trying to write a helper script to shorten some of my project commands.

The script looks like this:

import subprocess

def up():
  subprocess.call(['docker-compose', 'up'])

if __name__ == '__main__':
  up()

This works, however if I run ctrl + c, it will kill the script and not the subprocess.

Is there a way I can make the subprocess the priority for all terminal input until it exits?

martineau
  • 119,623
  • 25
  • 170
  • 301
LondonAppDev
  • 8,501
  • 8
  • 60
  • 87
  • `os.system()` probably does that. This doesn't particularly look like a good use case for a Python script, but perhaps your real script has more features. – tripleee Nov 01 '18 at 17:47
  • take a look at this: https://stackoverflow.com/questions/19447603/how-to-kill-a-python-child-process-created-with-subprocess-check-output-when-t – DorElias Nov 01 '18 at 17:51
  • 2
    You must catch `KeyboardInterrupt`, then. https://docs.python.org/3/library/exceptions.html#KeyboardInterrupt – Håken Lid Nov 01 '18 at 18:19
  • @tripleee appreciate the response. My goal is to use this as a helper script that is cross platform. Unfortunately I don't think there is a good x-platform alternative scripting language? – LondonAppDev Nov 02 '18 at 14:27

1 Answers1

0

I solved this as follows:

def up():
  try:
    subprocess.call(['docker-compose', 'up'])
  except KeyboardInterrupt:
    print('\n')

It appears to do the job.

LondonAppDev
  • 8,501
  • 8
  • 60
  • 87