Question

Let's say I run the following program from the terminal, and then immediately decide to stop it, I will have to hit control-c 5 times. How do I make it so one control-c will exit the entire program?

os.system("python run_me1.py -lines -s {0} -u {1}".format(args.start, args.until))
os.system("python run_me2.py -derivs -tt")
if args.mike: os.system("python run_me3.py -f derivs.csv tt.csv")
os.system("gnumeric derivs.csv")
os.system("gnumeric tt.csv")
Was it helpful?

Solution

Wrap it in a keyboard interrupt exception and replace os.system with subprocess.call.

Please not that for convenience of path resolution, I put the shell=True argument in, but that has security implications you should invalidate before doing this.

import subprocess

try:
    subprocess.call("python run_me1.py -lines -s {0} -u {1}".format(args.start, args.until), shell=True)
    subprocess.call("python run_me2.py -derivs -tt", shell=True)
    if args.mike: subprocess.call("python run_me3.py -f derivs.csv tt.csv", shell=True)
    subprocess.call("gnumeric derivs.csv", shell=True)
    subprocess.call("gnumeric tt.csv", shell=True)
except KeyboardInterrupt:
    print("exiting early")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top