From a896fba3e95e73b50c720e51723f2990074018c6 Mon Sep 17 00:00:00 2001 From: vuzlov Date: Thu, 17 Sep 2020 19:47:37 +0300 Subject: [PATCH] bos #16706 Improve procedure that kills booked TCP port --- bin/killSalome.py | 26 +++++------------- bin/killSalomeWithPort.py | 57 ++++++++++++++++++++++++++++++++++++--- bin/runSalome.py | 16 +++++++++++ 3 files changed, 76 insertions(+), 23 deletions(-) diff --git a/bin/killSalome.py b/bin/killSalome.py index 36c241158..d7b8c392e 100755 --- a/bin/killSalome.py +++ b/bin/killSalome.py @@ -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) diff --git a/bin/killSalomeWithPort.py b/bin/killSalomeWithPort.py index 66fd32671..2ffbdd42a 100755 --- a/bin/killSalomeWithPort.py +++ b/bin/killSalomeWithPort.py @@ -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() diff --git a/bin/runSalome.py b/bin/runSalome.py index 41eb05b03..172298f98 100755 --- a/bin/runSalome.py +++ b/bin/runSalome.py @@ -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""" -- 2.39.2