0

Below is my shellscript:

& 'C:\files\service_file.exe' install 


#Start the windows service
& 'C:\file\service_file.exe' start --env "D:\full_project\device\config\windows.env"  

I used pyinstaller to create service_file.exe and 'start' arg is working fine but does not support env path if included

I get path not found error, even after giving the env.

below is my service_file.py

import os
import win32serviceutil
import win32service
import servicemanager
import argparse

class MyService:
    def stop(self):
        self.running = False

    def run(self):
        application_path = os.path.dirname(sys.executable)
        os.chdir(application_path)

        def load_env(envFile):
            global env_obj
            env_obj = Environment(envFile)
            return env_obj

        parser = argparse.ArgumentParser(description="testing")
        parser.add_argument("--env")
        parser.add_argument("--log")
        args = parser.parse_args()
        env_obj = load_env(args.env)

        db_agent(env_obj)
        db_agent_download(env_obj)

        client_agent(env_obj)
        init_scheduler()

        app = rest_api(env_obj)
        app.run(port=env_obj.rest_api_port, host="0.0.0.0")


class MyServiceFramework(win32serviceutil.ServiceFramework):
    _svc_name_ = 'DeviceAgent'
    _svc_display_name_ = 'Device Agent Service'

    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        self.service_impl.stop()
        self.ReportServiceStatus(win32service.SERVICE_STOPPED)

    def SvcDoRun(self):
        self.service_impl = MyService()
        self.ReportServiceStatus(win32service.SERVICE_START_PENDING)
        self.ReportServiceStatus(win32service.SERVICE_RUNNING)
        self.service_impl.run()


def Main():

    if len(sys.argv) == 1:
        print("this is if")
        servicemanager.Initialize()
        servicemanager.PrepareToHostSingle(MyServiceFramework)
        servicemanager.StartServiceCtrlDispatcher()
    else:
        print("this is else",sys.argv)
        win32serviceutil.HandleCommandLine(MyServiceFramework)


if __name__ == '__main__':
    Main()

What should i do ? how can i run the shellscript along with env. I want to know how i should pass the argument for env

Titan
  • 244
  • 1
  • 4
  • 18
  • I don't see `start` in your argument parser code... – alec_djinn Dec 06 '22 at 14:46
  • Hi @alec_djinn Im using shellscript to pass the argument. you can see the 'start' there. – Titan Dec 06 '22 at 14:47
  • Why do you pass `start` as first argument then? Please, share the shell code as well. From what you have posted, it is unclear what `start` does – alec_djinn Dec 06 '22 at 14:48
  • @alec_djinn Actually that is the full shescript code in script.sh, upto my knowledge start need to be given at first to start the service in the windows services – Titan Dec 06 '22 at 14:51
  • Is the script working as intended when you run it with Python? `python service_file.py --env ...` – alec_djinn Dec 06 '22 at 14:53
  • @alec_djinn Yes, its working and if i remove the --env and leave with only start (gave path as static inside code without arg), then also working fine. but only if i include --env with start as above then --env not working. – Titan Dec 06 '22 at 14:55
  • Then it is not a Python problem, but one related with the windows shell. Unfortunately I can't help with that. – alec_djinn Dec 06 '22 at 14:56
  • @alec_djinn Hey thanks for your time ! NP, Hope I get some help from somebody. – Titan Dec 06 '22 at 14:57

0 Answers0