Skip to content

Commit

Permalink
Remove UnixCommandUtil.getPID
Browse files Browse the repository at this point in the history
This is no longer required.
Use Process.pid() instead (available since Java 9).
  • Loading branch information
hannesbraun committed Aug 30, 2023
1 parent a372937 commit ae4e964
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 40 deletions.
29 changes: 0 additions & 29 deletions src/main/java/hso/autonomy/util/misc/UnixCommandUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,13 @@
package hso.autonomy.util.misc;

import java.io.IOException;
import java.lang.reflect.Field;

/**
* A collection of command line utilities for direct unix commands.
* @author kdorer
*/
public class UnixCommandUtil
{
/**
* Is {@code true} if this is running on a Unix-like operating system.
* (Assumption: every operating system except for Windows is Unix-like.)
*/
private static boolean IS_UNIX_OS = !System.getProperty("os.name").startsWith("Windows");

/**
* Retrieves the pid of a unix process. (Since Java 9, this is also possible with {@link java.lang.Process#pid()}.)
* @param process the process object to get the pid from
* @return the pid, -1 if not able to get it
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public static int getPID(Process process)
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException
{
if (IS_UNIX_OS) {
/* get the PID on unix/linux systems */
Field f = process.getClass().getDeclaredField("pid");
f.setAccessible(true);
return f.getInt(process);
} else {
return -1;
}
}

/**
* Checks if the process with pid passed contains the passed name when run
* with ps. If so it is killed.
Expand Down
16 changes: 5 additions & 11 deletions src/main/java/magma/monitor/server/ServerController.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,11 @@ public boolean stopServer()

// we have started the server, so we also stop it
try {
int pid = UnixCommandUtil.getPID(serverProcess);
if (pid > 0) {
boolean killed = UnixCommandUtil.killProcessConditional("" + pid, "rcssserver3d");
if (!killed) {
System.err.println("Could not kill server! pid: " + pid + ", server: " + id);
// killAllServers();
}
} else {
System.err.println("Do not have pid of specific server, killing all"
+ ", server: " + id);
killAllServers();
long pid = serverProcess.pid();
boolean killed = UnixCommandUtil.killProcessConditional("" + pid, "rcssserver3d");
if (!killed) {
System.err.println("Could not kill server! pid: " + pid + ", server: " + id);
// killAllServers();
}
return true;

Expand Down

0 comments on commit ae4e964

Please sign in to comment.