22
22
# Boston, MA 02111-1307, USA.
24
24
from spectlib.watch import Watch
25
import spectlib.config
25
26
from spectlib.i18n import _
26
from spectlib.process import ProcessList
32
class Process_watch(Watch):
28
type = "Watch_system_process"
30
icon = 'applications-system'
32
def get_add_gui_info():
34
("process", spectlib.gtkconfig.Entry("Process"))
38
class Watch_system_process(Watch):
34
Watch class that will check if a file was modified, removed or created.
40
Watch class that will check if a process is running or not.
39
def __init__(self, refresh, process, specto, id, name = _("Unknown Process Watch")):
40
Watch.__init__(self, specto)
42
self.refresh = refresh
43
self.process = process
46
self.actually_updated=False
43
def __init__(self, specto, id, values):
46
( "process", spectlib.config.String(True) )
50
self.open_command = ''
51
self.type_desc = type_desc
54
#Init the superclass and set some specto values
55
Watch.__init__(self, specto, id, values, watch_values)
47
57
self.running = self.check_process()
49
def dict_values(self):
50
return { 'name': self.name, 'refresh': self.refresh, 'process': self.process, 'type':3 }
52
def start_watch(self):
53
""" Start the watch. """
56
def thread_update(self):
57
lock = thread.allocate_lock()
59
t=thread.start_new_thread(self.update,(lock,))
61
while gtk.events_pending():
64
while gtk.events_pending():
67
def update(self, lock):
68
""" See if a file was modified or created. """
70
self.specto.mark_watch_busy(True, self.id)
71
self.specto.logger.log(_("Updating watch: \"%s\"") % self.name, "info", self.__class__)
61
""" See if a file was modified or created. """
74
63
process = self.check_process()
75
64
if self.running and process == False:
76
65
self.running = False
77
66
self.updated = True
78
67
self.actually_updated = True
68
self.status = "Not running"
79
69
elif self.running == False and process == True:
80
70
self.running = True
81
71
self.actually_updated = True
82
else: self.actually_updated=False
72
self.status = "Running"
74
self.actually_updated=False
75
self.status = "Unknown"
85
78
self.specto.logger.log(_("Watch: \"%s\" has an error") % self.name, "error", self.__class__)
87
self.specto.mark_watch_busy(False, self.id)
88
Watch.update(self, lock)
80
Watch.timer_update(self)
90
82
def check_process(self):
91
83
""" see if the process is running or not """
99
def set_process(self, process):
100
self.process = process
91
def get_gui_info(self):
94
('Last updated', self.last_updated),
95
('Process', self.process),
96
('Status', self.status)
102
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick
104
Manage Processes and a ProcessList under Linux.
110
class Process(object):
111
"""Represents a process"""
112
def __init__(self, pid):
113
"""Make a new Process object"""
114
self.proc = "/proc/%d" % pid
115
pid,command,state,parent_pid = file(os.path.join(self.proc, "stat")).read().strip().split()[:4]
116
command = command[1:-1]
118
self.command = command
120
self.parent_pid = int(parent_pid)
123
def kill(self, sig = signal.SIGTERM):
124
"""Kill this process with SIGTERM by default"""
125
os.kill(self.pid, sig)
127
return "Process(pid = %r)" % self.pid
129
"""Read the current directory of this process or None for can't"""
131
return os.readlink(os.path.join(self.proc, "cwd"))
135
class ProcessList(object):
136
"""Represents a list of processes"""
138
"""Read /proc and fill up the process lists"""
141
for f in os.listdir("/proc"):
143
process = Process(int(f))
144
self.by_pid[process.pid] = process
145
self.by_command.setdefault(process.command, []).append(process)
146
for process in self.by_pid.values():
148
parent = self.by_pid[process.parent_pid]
149
parent.children.append(process)
150
process.parent = parent
153
def named(self, name):
154
"""Returns a list of processes with the given name"""
155
return self.by_command.get(name, [])
b'\\ No newline at end of file'