why and how to run forever and debug when error in for proc in psutil.process_iter()?
Ho Yeung Lee
jobmattcon at gmail.com
Sun Mar 26 07:40:09 EDT 2017
On Sunday, March 26, 2017 at 7:32:12 PM UTC+8, Ho Yeung Lee wrote:
> On Sunday, March 26, 2017 at 10:33:51 AM UTC+8, Deborah Swanson wrote:
> > Someone here can probably help you, but they'll need your Python
> > version, operating system, and full traceback. They get tired of saying
> > so.
> >
> > In this case, the full traceback is needed to see what went wrong and
> > when (after which statements).
> >
> >
> > Ho Yeung Lee wrote, on Saturday, March 25, 2017 1:38 AM
> > >
> > > expect below to run forever and keep running a fixed number
> > > of thread in python
> > >
> > > would like to kill tasks when process connect internet except
> > > chrome and explorer.exe
> > >
> > > i do this because MalwareBytes can not disconnect these
> > > existing trojan when my notebook connect internet
> > >
> > > after run a few minutes, the program stopped, but i have
> > > already kept create process, why the whole program end?
> > >
> > > why and how to debug when error in for proc in psutil.process_iter()?
> > >
> > >
> > > import os
> > > import psutil
> > > import multiprocessing
> > > import time
> > > import sys
> > >
> > > def cleantask():
> > > p = os.popen("netstat -ano")
> > > while 1:
> > > line = p.readline()
> > > if "TCP" in line or "UDP" in line:
> > > linelist = line.split()
> > > if len(linelist) > 4:
> > > if "LISTEN" in str(linelist[3]):
> > > for proc in psutil.process_iter():
> > > try:
> > > if "pop" not in str(proc.name).tolower():
> > > os.system("taskkill /f /pid
> > > "+str(proc._pid))
> > > except:
> > > dummy = 1
> > > #print "Unexpected error:",
> > > sys.exc_info()[0]
> > > #print "Unexpected error:",
> > > sys.exc_info()[1]
> > > if "ESTABLISHED" in str(linelist[3]):
> > > if "127.0.0.1" not in str(linelist[2]):
> > > for proc in psutil.process_iter():
> > > try:
> > > if str(linelist[4]) in
> > > str(proc._pid):
> > >
> > > print(str(linelist[2])+","+str(linelist[4])+","+proc.name)
> > > if "111.221" not in
> > > str(linelist[2]) and "explorer.exe" not in str(proc.name).tolower():
> > > os.system("taskkill /f
> > > /pid "+str(proc._pid))
> > > except:
> > > dummy = 1
> > > #print "Unexpected error:",
> > > sys.exc_info()[0]
> > > #print "Unexpected error:",
> > > sys.exc_info()[1]
> > > print(line)
> > > if not line: break
> > >
> > > if __name__ == '__main__':
> > > print("main")
> > > try:
> > > numberofrunning = 0
> > > plist = []
> > > for ii in range(0,5):
> > > p = multiprocessing.Process(target=cleantask(), args=(0,))
> > > p.start()
> > > plist.append(p)
> > > numberofrunning = numberofrunning + 1
> > > time.sleep(1)
> > > for pp in plist:
> > > pp.join()
> > > if pp.is_alive() == False:
> > > numberofrunning = numberofrunning - 1
> > > plist.remove(pp)
> > > if numberofrunning > 10:
> > > print "more than 10 process"
> > > else:
> > > print("number of process = " + str(numberofrunning))
> > > if numberofrunning <= 5:
> > > p =
> > > multiprocessing.Process(target=cleantask(), args=(0,))
> > > p.start()
> > > plist.append(p)
> > > numberofrunning = numberofrunning + 1
> > > time.sleep(1)
> > > except:
> > > print "Unexpected error:", sys.exc_info()[0]
> > > print "Unexpected error:", sys.exc_info()[1]
> > > --
> > > https://mail.python.org/mailman/listinfo/python-list
> > >
>
> after window update error, I can not login window and reset system and
> reinstall every thing
>
> python 2.7.12
>
> there is no error when run, but it end after running a few minutes
> if commend the forever loop in main
>
> import os
> import psutil
> import multiprocessing
> import time
> import sys
>
> def cleantask():
> bufsize = 0
> f = open("d:\\killlist.txt",'a',bufsize)
> p = os.popen("netstat -ano")
> while 1:
> line = p.readline()
> if "TCP" in line or "UDP" in line:
> linelist = line.split()
> if len(linelist) > 4:
> if "LISTEN" in str(linelist[3]):
> for proc in psutil.process_iter():
> try:
> if "pop" in str(proc.name).lower():
> os.system("taskkill /f /pid "+str(proc._pid))
> #print("here8")
> print(str(linelist[2])+","+str(linelist[4])+","+proc.name +" is killed ")
> #print("here9")
> path = proc.exe()
> print(str(linelist[2])+","+str(linelist[4])+","+proc.name +" at " + str(path))
> #print("here10")
> except:
> dummy = 1
> #print "Unexpected error:", sys.exc_info()[0]
> #print "Unexpected error:", sys.exc_info()[1]
> if "ESTAB" in str(linelist[3]):
> if "127.0.0.1" not in str(linelist[2]):
> for proc in psutil.process_iter():
> try:
> if str(linelist[4]) in str(proc._pid):
> #print("here1a")
> print(str(linelist[2])+","+str(linelist[4])+","+str(proc.name))
> #print("here2a")
> #print(str(linelist[2])+","+str(linelist[4])+","+proc.name)
>
> #print("here3a")
> if "111.221" in str(linelist[2]):
> dummy = 1
> elif "explorer.exe" in str(proc.name).lower():
> dummy = 1
> elif "svchost" in str(proc.name).lower():
> dummy = 1
> elif "cmd" in str(proc.name).lower():
> dummy = 1
> else:
> os.system("taskkill /pid "+str(proc._pid))
> #print("here1")
> print(str(linelist[2])+","+str(linelist[4])+","+str(proc.name) +" is killed ")
> #print("here2")
> path = proc.exe()
> print(str(linelist[2])+","+str(linelist[4])+","+str(proc.name) +" at " + str(path))
> #print("here3")
> f.write(str(linelist[2])+","+str(linelist[4])+","+str(proc.name) +" at " + str(path) +"\n")
> except:
> dummy = 1
> print "Unexpected error:", sys.exc_info()[0]
> print "Unexpected error:", sys.exc_info()[1]
> #print("here6")
> f.write(str(linelist[2])+","+str(linelist[4])+","+str(proc.name) +" can not kill at " + str(path) +"\n")
> #print("here7")
> f.flush()
> print(line)
> if not line: break
> f.close()
>
>
> if __name__ == '__main__':
> print("main")
> #while 1:
> try:
> numberofrunning = 0
> plist = []
> for ii in range(0,100):
> p = multiprocessing.Process(target=cleantask(), args=(0,))
> p.start()
> plist.append(p)
> numberofrunning = numberofrunning + 1
> time.sleep(1)
> for pp in plist:
> pp.join()
> if pp.is_alive() == False:
> numberofrunning = numberofrunning - 1
> plist.remove(pp)
> if numberofrunning > 100:
> print "more than 100 process"
> else:
> print("number of process = " + str(numberofrunning))
> if numberofrunning <= 90:
> p = multiprocessing.Process(target=cleantask(), args=(0,))
> p.start()
> plist.append(p)
> numberofrunning = numberofrunning + 1
> time.sleep(1)
> except:
> print "Unexpected error:", sys.exc_info()[0]
> print "Unexpected error:", sys.exc_info()[1]
another problem is'psutil.AccessDenied'
原因: 此為重要的系統處理程序。Taskkill 無法結束此處理程序。
204.79.197.200:443,1384,<bound method Process.name of <psutil.Process(pid=0, name='System Idle Process') at 62855096>> is killed
Unexpected error: <class 'psutil.AccessDenied'>
Unexpected error: psutil.AccessDenied (pid=0, name='System Idle Process')
Unexpected error: <type 'exceptions.UnboundLocalError'>
os.system("taskkill /f /pid "+str(proc._pid))
#print("here1")
More information about the Python-list
mailing list