Salome HOME
This commit was generated by cvs2git to create branch 'OCC_Config'.
[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         a = os.system("pid=`ps -eo pid,command | egrep \"[0-9] omniNames -start "+str(port)+"\" | sed -e \"s%[^0-9]*\([0-9]*\) .*%\\1%g\"`; kill -9 $pid >& /dev/null")
38         try:
39             process_ids=pickle.load(fpid)
40             fpid.close()
41             for process_id in process_ids:
42
43                 for pid, cmd in process_id.items():
44                     print "stop process %s : %s"% (pid, cmd[0])
45                     try:
46                         os.kill(int(pid),signal.SIGKILL)
47                     except:
48                         print "  ------------------ process %s : %s inexistant"% (pid, cmd[0])
49                         pass
50                 pass
51         except:
52             pass
53         os.remove(filedict)
54         pid = commands.getoutput("ps -eo pid,command | egrep \"[0-9] omniNames -start "+str(port)+"\" | sed -e \"s%[^0-9]*\([0-9]*\) .*%\\1%g\"")
55         a = ""
56         while pid != "" and len(a.split(" ")) < 2:
57             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")
58             pid = commands.getoutput("ps -eo pid,command | egrep \"[0-9] omniNames -start "+str(port)+"\" | sed -e \"s%[^0-9]*\([0-9]*\) .*%\\1%g\"")
59             print pid
60               
61
62 if __name__ == "__main__":
63     for port in sys.argv[1:]:
64         killMyPort(port)