0

I am making a python script that is meant to read from a config file that is placed in the same folder as the python script that I am working on. Inside the config file I have given the paths of the scripts that I need to run as process. These processes are scripts or modules that need to be run sequentially. In the Python script I have used the argparse library to make a cli to make it more user friendly and we can pass arguments such as start to start the scripts from the config file and stop to exit the scripts from the config file. I am also using the config parser library to read from the config file that I have created for the python script that I am working on.

The python script is as follows:

#!/bin/python3

import argparse as ap
import os
import configparser
import subprocess
import sys

def file_reader():
configParser = configparser.ConfigParser()

configParser.read('config.ini')
path1 = config.get('my-config','path1')
path2 = config.get('my-config','path2')
path3 = config.get('my-config','path3')
print(path1)
print(path2)
print(path3)

"""
def spawn_program_and_die():
    
    Start an external program and exit the script 
    with the specified return code.

    Takes the parameter program, which is a list 
    that corresponds to the argv of your command.
    
    # Start the external program
    subprocess.Popen(path1)
    # We have started the program, and can suspend this interpreter
    sys.exit(exit_code)
"""

def main():
parser = ap.ArgumentParser{description='Gathers parameters'}
parser.add_argumentparser = argparse.ArgumentParser()
parser.add_argument('-s', "--start",help="Start script")
parser.add_argument('-q', "--stop", help="Stop script" )
args = parser.parse_args()

if args.start and args.stop:
    print 'error'
elif args.start:
    subprocess.Popen(['path1', start], shell=True)
    subprocess.Popen(['path2', start], shell=True)
    subprocess.Popen(['path3', start], shell=True)
elif args.stop: 
    subprocess.Popen(['path1', stop], shell=True)
    subprocess.Popen(['path2', stop], shell=True)
    subprocess.Popen(['path3', stop], shell=True)
    
if __name__ == '__main__':
    main()

My config file which contains the paths to other scripts is as follows:

[my-config]
path1 = "/home/user/script1"
path2 = "/home/user/script2"
path3 = "/home/user/script3"

I am not able to run it sequentially. Can someone please guide?

Asad Javed
  • 57
  • 6
  • Anyone? Help will be really appreciated – Asad Javed Sep 06 '21 at 16:46
  • Does this answer your question? [Python popen command. Wait until the command is finished](https://stackoverflow.com/questions/2837214/python-popen-command-wait-until-the-command-is-finished) – Lenormju Sep 08 '21 at 14:04

0 Answers0