CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
cmsPerfSuiteKill.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #Utility script to kill with one command any cmsPerfSuite.py related process running.
3 ######NOTE######
4 #USE AT YOUR OWN RISK!
5 #THIS SCRIPT WILL KILL ANY PROCESS THE USER HAS RUNNING THAT COULD BE PART OF
6 #THE PERFORMANCE SUITE EXECUTION!
7 import subprocess,os,sys,cmsScimarkStop,time
8 user=os.environ['USER']
9 
10 def main():
11  exitcode=0
12  #First invoke cmsScimarkStop.py to stop eventual cmsScimarks running...
14 
15  #List of executables to spot in ps -ef and kill:
16  scripts=['cmsPerfSuite.py',
17  'cmsRelvalreportInput.py',
18  'cmsRelvalreport.py',
19  'cmsDriver.py',
20  'cmsRun',
21  'cmsScimark2',
22  'cmsIgProf_Analysis.py',
23  'igprof-analyse',
24  'perfreport'
25  ]
26 
27 
28  print "Looking for processes by user %s"%user
29  checkProcesses=subprocess.Popen("ps -efww|grep %s"%user,bufsize=4096,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
30  for line in checkProcesses.stdout:
31  for executable in scripts:
32  #print "Looking for %s script"%executable
33  if executable in line:
34  print "Found process %s"%line
35  print "Killing it!"
36  PID=line.split()[1]
37  kill_stdouterr=subprocess.Popen("kill %s"%PID,shell=True,stdout=subprocess.PIPE, stderr=subprocess.STDOUT).stdout.read()
38  print kill_stdouterr
39 
40  #There could be a few more processes spawned after some of the killings... give it a couple of iterations:
41  i=0
42  while i<100:
43  exitcode=0
44  checkProcesses=subprocess.Popen("ps -efww|grep %s"%user,bufsize=4096,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
45  for line in checkProcesses.stdout:
46  for executable in scripts:
47  if executable in line:
48  print "Something funny going on! It seems I could not kill process:\n%s"%line
49  print "SORRY!"
50  exitcode=-1
51  if exitcode>=0:
52  print "Finally killed all jobs!"
53  break
54  i+=1
55  time.sleep(2)
56  #Return 0 if all killed, could use the -1 exit code if we decide to put a limit to the number of cycles...
57  return exitcode
58 
59 if __name__ == "__main__":
60  sys.exit(main())