简体   繁体   中英

i want to run my python program(not script) with start command which is a method in that python program file to be executed

Hi have python program in which a start method is defined, in start method i am calling a win32serviceutil.StartService(service) method to start a service, like

import os, platform, subprocess

try:
    import win32serviceutil
except:
    os.system("pip install pywin32")
    os.system("pip install pypiwin32")
    import win32serviceutil

OS = platform.system() #will get you the platform/OS
print("You are using ", OS)

if __name__=='__main__':

    service = 'WSearch'
    def startByCLI():
        cmd = 'net start '+service
        os.system(cmd)

    def startByPython():
        # subprocess.check_output(["sc", "start", service], stderr=subprocess.STDOUT)
        win32serviceutil.StartService(service)

    if OS=='Windows':
        try:
            output = startByPython()
        except subprocess.CalledProcessError as e:
            print(e.output)
            print(e.returncode)
            #os.system('python test2.py')
            subprocess.call("python ./install.py asadmin", shell=True)
            startByCLI()

so what i actually want is i want to run the start method from command promt like this

python ./myfile.py startByPython

and it will trigger the startByPython method in myfile.py

many thanks in advance

Hey all thanks for your attention, i wanted to run my file.py file with argument from command line like:

$ /usr/bin/python myfile.py start

i got the solution which is

  def main(): # read arguments from the command line and # check whether at least two elements were entered if len(sys.argv) < 2: print "Usage: python aws.py {start|stop}\\n" sys.exit(0) else: action = sys.argv[1] 
   if action == "start":
      startInstance()
   elif action == "stop":
      stopInstance()
   else:
      print "Usage: python aws.py {start|stop}\n"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM