Skip to content

Commit eef7d7e

Browse files
Refactor: 2nd Update ShutdownCommand.java
1 parent b093d0e commit eef7d7e

1 file changed

Lines changed: 14 additions & 6 deletions

File tree

src/main/java/com/mycmd/commands/ShutdownCommand.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ public void execute(String[] args, ShellContext context) throws IOException {
4444
System.out.println("Shutdown cancelled.");
4545
return;
4646
}
47-
}
4847
}
48+
}
4949

5050
try {
5151
StringBuilder cmdBuilder = new StringBuilder("shutdown");
@@ -63,15 +63,23 @@ public void execute(String[] args, ShellContext context) throws IOException {
6363
}
6464

6565
Process process = pb.start();
66-
try (java.util.Scanner scanner = new java.util.Scanner(System.in)) {
67-
String confirmation = scanner.nextLine().trim().toLowerCase();
66+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
67+
BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
68+
String line;
69+
while ((line = reader.readLine()) != null) {
70+
System.out.println(line);
71+
}
6872

69-
if (!confirmation.equals("yes")) {
70-
System.out.println("Shutdown cancelled.");
71-
return;
73+
while ((line = errorReader.readLine()) != null) {
74+
System.err.println(line);
7275
}
7376
}
7477

78+
if (!process.waitFor(30, TimeUnit.SECONDS)) {
79+
process.destroyForcibly();
80+
System.out.println("Command timed out.");
81+
}
82+
7583
} catch (Exception e) {
7684
System.out.println("Error executing shutdown: " + e.getMessage());
7785
System.out.println("Administrator privileges may be required.");

0 commit comments

Comments
 (0)