External CLI tool to demo subprocess


The process.py is a simple script we are going to use to demonstrate how an external program can be executed from within Python. It is a Python program, but you could do the exact same thing with any command-line application written in any language. We use this Python script as an example because we know you already have Python on your computer.

The external command:


examples/process/process.py
import time
import sys

if len(sys.argv) != 3:
    exit(f"{sys.argv[0]} SECONDS EXIT_CODE")

seconds = int(sys.argv[1])
exit_code = int(sys.argv[2])

for sec in range(seconds):
   print("OUT {}".format(sec), flush=True)
   print("ERR {}".format(sec), file=sys.stderr)
   time.sleep(1)

exit(exit_code)

Try it on the command line: python process.py 3 7