Salome HOME
Implemented a possibility to dlopen SalomeDS libarary and create SObject, Study,...
[modules/kernel.git] / bin / killSalomeWithPort.py
1 #!/usr/bin/env python
2 import os, sys, pickle, signal, commands
3
4 def getPiDict(port,appname='salome',full=True):
5     from Utils_Identity import getShortHostName
6
7     host = os.getenv("HOSTNAME")
8     if not host:
9         host = os.getenv("HOST")
10     if not host:
11         host = getShortHostName()
12
13     filedict = []
14     filedict.append( os.getenv('USER') )          # user name
15     filedict.append( host )                       # host name
16     filedict.append( str(port) )                  # port number
17     filedict.append( appname.upper() )            # application name
18     filedict.append( 'pidict' )                   # constant part
19
20     filedict = '_'.join(filedict)
21     if full:
22         filedict = os.getenv("HOME") + '/' + filedict
23     return filedict
24
25 ########## kills all salome processes with the given port ##########
26 def killMyPort(port):
27     filedict=getPiDict(port)
28     found = 0
29     try:
30         fpid=open(filedict, 'r')
31         found = 1
32     except:
33         print "le fichier %s des process SALOME n'est pas accessible"% filedict
34         pass
35         
36     if found:
37         cmd = 'pid=`ps -eo pid,command | egrep "[0-9] omniNames -start '+str(port)+'"` ; echo $pid > /tmp/logs/'+os.getenv('USER')+"/_"+port+'_Pid_omniNames.log'
38         a = os.system(cmd)
39         try:
40             fpidomniNames=open('/tmp/logs/'+os.getenv('USER')+"/_"+port+'_Pid_omniNames.log')
41             prc = fpidomniNames.read()
42             fpidomniNames.close()
43             if prc != None :
44                 for field in prc.split(" ") :
45                     if field == "omniNames" :
46                         if pidfield != "egrep" :
47                             print 'stop process '+pidfield+' : omniNames'
48                             os.system('kill -9 '+pidfield)
49                     pidfield = field
50         except:
51             pass
52         try:
53             process_ids=pickle.load(fpid)
54             fpid.close()
55             for process_id in process_ids:
56
57                 for pid, cmd in process_id.items():
58                     print "stop process %s : %s"% (pid, cmd[0])
59                     try:
60                         os.kill(int(pid),signal.SIGKILL)
61                     except:
62                         print "  ------------------ process %s : %s inexistant"% (pid, cmd[0])
63                         pass
64                 pass
65         except:
66             pass
67         os.remove(filedict)
68         pid = commands.getoutput("ps -eo pid,command | egrep \"[0-9] omniNames -start "+str(port)+"\" | sed -e \"s%[^0-9]*\([0-9]*\) .*%\\1%g\"")
69         a = ""
70         while pid != "" and len(a.split(" ")) < 2:
71             a = commands.getoutput("pid=`ps -eo pid,command | egrep \"[0-9] omniNames -start "+str(port)+"\" | sed -e \"s%[^0-9]*\([0-9]*\) .*%\\1%g\"`; kill -9 $pid")
72             pid = commands.getoutput("ps -eo pid,command | egrep \"[0-9] omniNames -start "+str(port)+"\" | sed -e \"s%[^0-9]*\([0-9]*\) .*%\\1%g\"")
73             print pid
74               
75
76 if __name__ == "__main__":
77     for port in sys.argv[1:]:
78         killMyPort(port)