-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKillProcess.java
130 lines (96 loc) · 3.07 KB
/
KillProcess.java
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
package me.malik.app.killer;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.swing.JOptionPane;
public final class KillProcess {
private static final String PIDS_CMD = "ps aux";
private static final String TARGET_APP = "spotify";
private static final String TERMINATE_APP = "kill ";
private KillProcess() { }
public final static class Executor {
private String command;
public Executor(String command) {
this.command = command;
}
public String getCommand() {
return this.command;
}
public void setCommand(String cmd) {
this.command = cmd;
}
public Process exec() throws IOException {
return Runtime.getRuntime().exec(this.command);
}
}
public final static class StdInput {
private InputStream inStream;
private InputStream errStream;
private final Executor exec;
public StdInput(Process proc, Executor exec) {
this.inStream = proc.getInputStream();
this.errStream = proc.getErrorStream();
this.exec = exec;
}
public InputStream getInputStream() {
return this.inStream;
}
public InputStream getErrorStream() {
return this.errStream;
}
public void setInputStream(Process in) {
this.inStream = in.getInputStream();
this.errStream = in.getErrorStream();
}
public void terminate() throws IOException {
BufferedReader bufIn = new BufferedReader(new InputStreamReader(this.inStream));
BufferedReader bufErr = new BufferedReader(new InputStreamReader(this.errStream));
System.out.println("Generating Output...");
String res = null;
String content = null;
while ((res = bufIn.readLine()) != null) {
if (res.toLowerCase().contains(TARGET_APP)) {
content = res;
}
System.out.println(res);
}
System.out.println("Generating Error Output...");
while ((res = bufErr.readLine()) != null) {
System.out.println(res);
}
int pid = 0;
if (content == null) {
JOptionPane.showMessageDialog(null, "Could not find Process for: " + TARGET_APP.toUpperCase(), null, JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
if (content.matches(".*\\d.*")) {
pid = Integer.parseInt(KillProcess.extractNumber(content));
}
if (pid != 0) {
System.out.println(pid);
this.exec.setCommand(TERMINATE_APP + String.valueOf(pid));
this.exec.exec();
}
}
}
public static void main(String[] args) throws IOException {
Executor controller = new Executor(PIDS_CMD);
StdInput inputController = new StdInput(controller.exec(), controller);
inputController.terminate();
}
public static String extractNumber(final String str) {
if(str == null || str.isEmpty()) return "";
StringBuilder sb = new StringBuilder();
boolean found = false;
for (char c : str.toCharArray()) {
if (Character.isDigit(c)) {
sb.append(c);
found = true;
} else if (found) {
break;
}
}
return sb.toString();
}
}