-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocman.rb
More file actions
executable file
·185 lines (172 loc) · 5.09 KB
/
Copy pathprocman.rb
File metadata and controls
executable file
·185 lines (172 loc) · 5.09 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/env ruby
#
# ProcManRuby - Simple Process Manager
# This script allows you to manage processes on a Unix-like system.
# It provides functionality to list, pause, resume, and kill processes.
# It also logs actions to a file.
#
LOG_FILE = 'procman.log'
def log_action(action, pid = nil)
timestamp = Time.now.strftime("%Y-%m-%d %H:%M:%S")
log_message = "#{timestamp} - #{action}"
log_message += " - PID: #{pid}" if pid
File.open(LOG_FILE, 'a') do |log|
log.puts(log_message)
end
end
def process_exists?(pid)
pid = pid.to_i
return false if pid <= 0
begin
# send signal 0 to check if the process exists does not actually send a signal
Process.kill(0, pid)
true
rescue Errno::ESRCH
false
rescue Errno::EPERM
true
end
end
def list_processes
command = "ps -eo pid,user,%cpu,%mem,etime,state,comm"
output = `#{command}`
log_action("Listagem de processos")
puts output
end
def pause_process(pid)
if process_exists?(pid.to_i)
begin
Process.kill("STOP", pid.to_i)
puts "Paused process with PID #{pid}"
log_action("Paused process", pid)
rescue Errno::EPERM
puts "Error: permission denied. Can't stop the process` #{pid}."
log_action("Error pausing process (permission denied)", pid)
rescue => e
puts "Error while pausing the process #{pid}: #{e.message}"
log_action("Error pausing process", pid)
end
else
puts "Process with PID #{pid} does not exist."
log_action("Error pausing process (not found)", pid)
end
end
def resume_process(pid)
if process_exists?(pid.to_i)
begin
Process.kill("CONT", pid.to_i)
puts "Resumed process with PID #{pid}"
log_action("Resumed process", pid)
rescue Errno::EPERM
puts "Error: permission denied. Can't resume the process #{pid}."
log_action("Error resuming process (permission denied)", pid)
rescue => e
puts "Error while resuming the process #{pid}: #{e.message}"
log_action("Error resuming process", pid)
end
else
puts "Process with PID #{pid} does not exist."
log_action("Error resuming process (not found)", pid)
end
end
def kill_process(pid)
if process_exists?(pid.to_i)
begin
Process.kill("TERM", pid.to_i)
puts "Killed process with PID #{pid}"
log_action("Killed process", pid)
rescue Errno::EPERM
puts "Error: permission denied. Can't kill the process #{pid}."
log_action("Error killing process (permission denied)", pid)
rescue => e
puts "Error while killing the process #{pid}: #{e.message}"
log_action("Error killing process", pid)
end
else
puts "Process with PID #{pid} does not exist."
log_action("Error killing process (not found)", pid)
end
end
def show_process_info(pid)
if process_exists?(pid.to_i)
command = "ps -p #{pid} -o pid,user,%cpu,%mem,etime,state,args"
output = `#{command}`
if output.empty? || output.include?("ERROR")
puts "Failed to retrieve information for PID #{pid}. It might be a kernel thread, zombie, or incompatible system."
log_action("Error showing process info (no data)", pid)
else
puts output
log_action("Show process info", pid)
end
else
puts "Process with PID #{pid} does not exist."
log_action("Error showing process info (not found)", pid)
end
end
def interactive_mode
loop do
puts "ProcManRuby - Interactive Mode"
puts "Enter a command (list, pause, resume, kill, info, exit):"
command = STDIN.gets.chomp #fix this to use STDIN instead of gets(chomp)
case command
when 'list'
list_processes
when 'pause'
puts "Enter PID to pause:"
pid = STDIN.gets.chomp
pause_process(pid)
when 'resume'
puts "Enter PID to resume:"
pid = STDIN.gets.chomp
resume_process(pid)
when 'kill'
puts "Enter PID to kill:"
pid = STDIN.gets.chomp
kill_process(pid)
when 'info'
puts "Enter PID to show info:"
pid = STDIN.gets.chomp
show_process_info(pid)
when 'exit'
puts "Exiting interactive mode."
log_action("Exited interactive mode")
break
else
puts "Unknown command."
puts "Available commands: list, pause, resume, kill, info, exit"
end
end
end
def show_help
puts "Usage: procman.rb [command] [pid]"
puts "Commands:"
puts " list List all processes"
puts " pause Pause a process"
puts " resume Resume a process"
puts " kill Kill a process"
puts " help Show this help message"
puts "Examples:"
puts " procman.rb list"
puts " procman.rb pause 1234"
puts " procman.rb resume 1234"
puts " procman.rb kill 1234"
end
if ARGV.empty? || ARGV[0] == 'help'
show_help
elsif ARGV[0] == 'list'
list_processes
elsif ARGV[0] == 'pause' && ARGV[1]
pause_process(ARGV[1])
elsif ARGV[0] == 'resume' && ARGV[1]
resume_process(ARGV[1])
elsif ARGV[0] == 'kill' && ARGV[1]
kill_process(ARGV[1])
elsif ARGV[0] == 'info' && ARGV[1]
show_process_info(ARGV[1])
elsif ARGV[0] == 'interactive'
log_action("Entered interactive mode")
interactive_mode
else
puts "Command not found."
show_help
end