areed1192 / interactive-broker-python-api

A python packaged used to interact with the Interactive Brokers REST API.
MIT License
374 stars 122 forks source link

Too bad it is no longer update. I like it is simple and direct. I have added starting IB gateway in Ubuntu if anyone like it too. #33

Open tonsmons opened 1 year ago

tonsmons commented 1 year ago

client.py

@@ -527,8 +552,37 @@
                 args=IB_WEB_API_PROC,
                 cwd=self.client_portal_folder
             ).pid
-
-        return self.server_process
+
+        elif self._operating_system == 'linux':
+            IB_WEB_API_PROC = [self.client_portal_folder+r"/bin/run.sh",r"root/conf.yaml"]
+            process = subprocess.Popen(
+                args=IB_WEB_API_PROC,
+                cwd=self.client_portal_folder,
+                stdout=subprocess.PIPE, universal_newlines=True
+            ).pid
+            time.sleep(2)
+            # get the right process by psutil, the above process takes the pid from run.sh, not the java one
+            for proc in psutil.process_iter():
+                try:
+                    processID = proc.pid
+                    # processName = proc.name()
+                    cmd = proc.cmdline()
+                    if any(r"root/conf.yaml" in s for s in cmd) and not any('run.sh' in s for s in cmd):
+                        self.server_process = processID
+                        break
+                except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
+                    pass
+            # keep alive process
+            IB_WEB_ALIVE = r'while true; do /usr/bin/sleep 300; /usr/bin/curl -ks '+self.ib_gateway_path+'/v1/portal/tickle; done'
+            self.server_alive = subprocess.Popen(args=[IB_WEB_ALIVE], shell=True).pid
+
+        return self.server_process,self.server_alive