]> SALOME platform Git repositories - modules/kernel.git/commitdiff
Salome HOME
bos #16706 Improve procedure that kills booked TCP port
authorvuzlov <vuzlov@opencascade.com>
Thu, 17 Sep 2020 16:47:37 +0000 (19:47 +0300)
committervsr <vsr@opencascade.com>
Fri, 9 Oct 2020 15:23:13 +0000 (18:23 +0300)
bin/killSalome.py
bin/killSalomeWithPort.py
bin/runSalome.py

index 36c2411584d23682bee1ebe7bf6ffa08f291cb4f..d7b8c392eaf975767e73af7294a858aa47414203 100755 (executable)
@@ -28,7 +28,7 @@
 
 import os, sys, re, signal
 
-from killSalomeWithPort import killMyPort, getPiDict
+from killSalomeWithPort import killMyPort, getPiDict, checkUnkilledProcess
 #from salome_utils import getHostName, getShortHostName
 from salome_utils import getUserName
 
@@ -81,27 +81,13 @@ def killAllPorts():
     except:
         pass
     # kill other processes
-    if sys.platform != 'win32':
-        import subprocess
-        cmd = "ps -fea | grep '%s' | grep 'ghs3d' | grep 'f /tmp/GHS3D_' | grep -v 'grep' | awk '{print $2}'" % user
-        prc = subprocess.getoutput(cmd)
-        for field in prc.split():
-            try:
-                os.kill(int(field), signal.SIGKILL)
-            except:
-                pass
-            pass
-        pass
-        # kill ompi-server needed for MPI containers coupling
-        cmd = "ps -fea | grep '%s' | grep 'ompi-server' | grep -v 'grep' | awk '{print $2}'" % user
-        prc = subprocess.getoutput(cmd)
-        for field in prc.split():
-            try:
-                os.kill(int(field), signal.SIGKILL)
-            except:
-                pass
+    for pid in checkUnkilledProcess():
+        try:
+            os.kill(pid, signal.SIGKILL)
+        except:
             pass
         pass
+    if sys.platform != 'win32':
         # delete uri files needed by ompi-server
         cmd = "rm -f " + os.path.expanduser("~") + "/.urifile_*"
         os.system(cmd)
index 66fd32671d5bc2da8223bdbe7f8c1ccbfb1d00b3..2ffbdd42a5876e5f57d3d1a426fdd1655a448ec1 100755 (executable)
@@ -31,7 +31,7 @@
 #  \endcode
 #
 
-import os, sys, pickle, signal, subprocess,glob
+import re, os, sys, pickle, signal, glob
 import subprocess
 import shlex
 from salome_utils import verbose
@@ -237,12 +237,18 @@ def __killMyPort(port, filedict):
             process_ids=pickle.load(fpid)
             for process_id in process_ids:
                 for pid, cmd in list(process_id.items()):
+                    pid = int(pid)
                     if verbose(): print("stop process %s : %s"% (pid, cmd[0]))
                     try:
                         from salome_utils import killpid
-                        killpid(int(pid))
+                        killpid(pid)
                     except:
                         if verbose(): print("  ------------------ process %s : %s not found"% (pid, cmd[0]))
+                        if pid in checkUnkilledProcess():
+                            try:
+                                killpid(pid)
+                            except:
+                                pass
                         pass
                     pass # for pid ...
                 pass # for process_id ...
@@ -360,7 +366,7 @@ def killMyPortSpy(pid, port):
     dt = 1.0
     while 1:
         from salome_utils import killpid
-        ret = killpid(int(pid), 0)
+        ret = killpid(pid, 0)
         if ret == 0:
             break
         elif ret < 0:
@@ -394,6 +400,41 @@ def killMyPortSpy(pid, port):
     killMyPort(port)
     return
 
+def checkUnkilledProcess():
+    #check processes in system after kill
+    from salome_utils import getUserName
+    user = getUserName()
+    processes = dict()
+
+    if sys.platform != 'win32':
+
+        def _getDictfromOutput(_output, _dic, _cond = None):
+            _pids = dict(zip(list(map(int, _output[::2])), _output[1::2]))
+            if _cond:
+                _pids = {pid:cmd for pid,cmd in _pids.items() if re.match(_cond, cmd)}
+            _dic.update(_pids)
+
+        # 1. SALOME servers plus omniNames
+        cmd = 'ps --noheading -U {user} -o pid,cmd | awk \'{{printf("%s %s\\n", $1, $2)}}\''.format(user=user)
+        _getDictfromOutput(subprocess.getoutput(cmd).split(), processes, '^(SALOME_|omniNames)')
+        # 2. ghs3d
+        cmd = 'ps -fea | grep \'{user}\' | grep \'ghs3d\' | grep \'f /tmp/GHS3D_\' | grep -v \'grep\' | awk \'{{print("%s %s\\n", $2, $8)}}\''.format(user=user)
+        _getDictfromOutput(subprocess.getoutput(cmd).split(), processes)
+        # 3. ompi-server
+        cmd = 'ps -fea | grep \'{user}\' | grep \'ompi-server\' | grep -v \'grep\' | awk \'{{print("%s %s\\n", $2, $8)}}\''.format(user=user)
+        _getDictfromOutput(subprocess.getoutput(cmd).split(), processes)
+    else:
+        cmd = 'tasklist /fo csv | findstr /i "SALOME_ omniNames"'
+        prc = subprocess.getoutput(cmd)
+        try:
+            prc = prc.split()
+            prc = [prc[i].split(',') for i in range(0, len(prc)) if i % 2 == 0]
+            prc = dict([(int(prc[j][1].replace('"', '')), prc[j][0].replace('"', '')) for j in range(0, len(prc))])
+            processes.update(prc)
+        except:
+            pass
+    return processes
+
 if __name__ == "__main__":
     if len(sys.argv) < 2:
         print("Usage: ")
@@ -410,6 +451,16 @@ if __name__ == "__main__":
             pass
         sys.exit(0)
         pass
+    elif sys.argv[1] == "--find":
+        processes = checkUnkilledProcess()
+        if processes:
+            print("Unkilled processes: ")
+            print(" --------------------")
+            print(" PID   : Process name")
+            print(" --------------------")
+            for pair in processes.items():
+                print('%6d : %s' % pair)
+        sys.exit(0)
     try:
         from salomeContextUtils import setOmniOrbUserPath #@UnresolvedImport
         setOmniOrbUserPath()
index 41eb05b03314f1fb7f03009d39604729f7b34509..172298f984413af787eb7b8aa69b5abac9e276ef 100755 (executable)
@@ -480,6 +480,7 @@ def startSalome(args, modules_list, modules_root_dir):
     #
 
     clt=orbmodule.client(args)
+    addToPidict(args)
 
     #
     # Wake up session option
@@ -519,6 +520,7 @@ def startSalome(args, modules_list, modules_root_dir):
         myServer=LoggerServer(args)
         myServer.run()
         clt.waitLogger("Logger")
+        addToPidict(args)
 
     # Launch  Session Server (to show splash ASAP)
     #
@@ -527,6 +529,7 @@ def startSalome(args, modules_list, modules_root_dir):
         mySessionServ = SessionServer(args,args['modules'],modules_root_dir)
         mySessionServ.setpath(modules_list,modules_root_dir)
         mySessionServ.run()
+        addToPidict(args)
 
     #
     # Launch Registry Server,
@@ -540,6 +543,7 @@ def startSalome(args, modules_list, modules_root_dir):
           clt.waitNS("/Registry")
         else:
           clt.waitNSPID("/Registry",myServer.PID)
+        addToPidict(args)
 
     #
     # Launch Catalog Server,
@@ -555,6 +559,7 @@ def startSalome(args, modules_list, modules_root_dir):
           clt.waitNS("/Kernel/ModulCatalog",SALOME_ModuleCatalog.ModuleCatalog)
         else:
           clt.waitNSPID("/Kernel/ModulCatalog",cataServer.PID,SALOME_ModuleCatalog.ModuleCatalog)
+        addToPidict(args)
 
     #
     # Launch SalomeDS Server,
@@ -570,6 +575,7 @@ def startSalome(args, modules_list, modules_root_dir):
           clt.waitNS("/Study")
         else:
           clt.waitNSPID("/Study",myServer.PID)
+        addToPidict(args)
 
     #
     # Launch LauncherServer
@@ -579,6 +585,7 @@ def startSalome(args, modules_list, modules_root_dir):
       myCmServer = LauncherServer(args)
       myCmServer.setpath(modules_list,modules_root_dir)
       myCmServer.run()
+      addToPidict(args)
 
     #
     # Launch ConnectionManagerServer
@@ -610,6 +617,7 @@ def startSalome(args, modules_list, modules_root_dir):
           clt.waitNS("/Containers/" + theComputer + "/FactoryServer")
         else:
           clt.waitNSPID("/Containers/" + theComputer + "/FactoryServer",myServer.PID)
+        addToPidict(args)
 
     if 'pyContainer' in args['standalone']:
         raise Exception('Python containers no longer supported')
@@ -805,6 +813,14 @@ def no_main():
 
 # -----------------------------------------------------------------------------
 
+def addToPidict(args):
+    global process_id
+    from addToKillList import addToKillList
+    for pid, cmd in list(process_id.items()):
+        addToKillList(pid, cmd, args['port'])
+
+# -----------------------------------------------------------------------------
+
 def main(exeName=None):
     """Salome launch as a main application"""