1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
#!/usr/bin/env python
Refresh = 15
ExtendedInfo = True
BlockList = [ "bash", "x", "xinit", "startx", "firefox-bin", "fluxbox" ]
UniqueList = [ "teamspeak.bin", "xmms", "pymacs-services", "dcpp", "gftp-gtk"]
Output = "~/.fluxbox/fbprocmenu.txt"
#------------------------ process info script for fluxbox menu ------------------
import sys, os
from os.path import isdir, isfile, join, expanduser
from time import sleep
def getprocesses():
"""Sort out process ids from /proc"""
proccontent = os.listdir("/proc")
dirsonly = [d for d in proccontent if isdir(join("/proc", d))]
proclist = [elem for elem in dirsonly if elem[0] in ['1', '2', '3', '4', '5', '6', '7', '8', '9']]
proclist.sort()
return proclist
def getprocinfo(procid):
"""Get info about an individual process."""
procinfo = {}
procpath = "/proc/%s/status" % procid
statfile = open(procpath, 'r')
procinfo["pid"] = procid
procinfo["uid"] = 0
for line in statfile:
if "Name" in line:
procinfo["Name"] = line.split(":")[1].strip()
elif "State" in line:
procinfo["State"] = line.split(":")[1].split(" ")[0].strip()
elif "Uid" in line:
procinfo["uid"] = int(line.split(":")[1].strip().split("\t")[0])
elif "Gid" in line:
break
statfile.close()
return procinfo
def makeprocdictlist(proclist):
"""Construct list of procinfo dictionaries"""
uid = os.getuid()
procdictlist = []
duplicatelist = []
for pid in proclist:
try:
pf = getprocinfo(pid)
if pf['uid'] == uid:
name = pf['Name'].lower()
if not name in BlockList:
if name in UniqueList:
if not name in duplicatelist:
duplicatelist.append(name)
procdictlist.append(pf)
else:
procdictlist.append(pf)
except IOError:
pass
procdictlist.sort(lambda a,b: cmp(a['Name'].lower(), b['Name'].lower()))
return procdictlist
def setextended(list):
user = os.getenv("USER")
cmd = "ps aux | awk '/"+user+"/ { printf("%s %s %s",$2,$8,$9); for(i=11;i<=NF;i++) printf(" %s",$i); printf("\\n") }'"
stdin, stdout = os.popen2(cmd)
info = {}
for line in stdout:
lst = line.split(' ')
cmdline = " ".join(lst[3:]).strip()
info[lst[0]] = {'mode':lst[0], 'start':lst[2], 'cmd':cmdline}
for elem in list:
try:
elem.update(info[elem['pid']])
except Exception: pass
while True:
Output = expanduser(Output)
procs = getprocesses()
info = makeprocdictlist(procs)
if ExtendedInfo: setextended(info)
try:os.remove(Output)
except OSError: pass
try:
outfile = open(Output, 'w+')
outfile.write('[begin] (procinfo) {}\n')
if ExtendedInfo:
for elem in info:
try:
line = "[submenu] ("+elem['start']+' '+elem["Name"]+" "+elem["mode"]+") {}\n"
line+= " [exec] (kill -9 "+elem["pid"]+") {kill -9 "+elem["pid"]+"}\n"
line+= " [exec] (cmd: "+elem['cmd']+") {}\n"
line+= "[end]\n"
except KeyError:
line = "[submenu] ( --:-- "+elem["Name"]+" "+elem["State"]+") {}\n [exec] (kill -9 "+elem["pid"]+") {kill -9 "+elem["pid"]+"}\n[end]\n"
outfile.write(line)
else:
for elem in info:
line = "[submenu] ("+elem["Name"]+" "+elem["pid"]+" "+elem["State"]+") {}\n [exec] (kill) {kill -9 "+elem["pid"]+"}\n[end]\n"
outfile.write(line)
outfile.close()
sleep(Refresh)
except IOError:
print "Cannot create "+Output+" file."
sys.exit(0)
|