Good all the time of day.
Problem: When running a large number of different background Java applications, there is a problem of their identification when using ps / top, because as a result, we have a large number of processes with the name “java”, which is not convenient.
There are several solutions to the problem:
- Writing JNI wrapper
- Own decision
I solved this problem as follows.
int main (int argc, char ** argv)
{
char * java_home = getenv ("JAVA_HOME");
const char * java_bin_affix = "/ bin / java";
if (java_home == NULL) {
printf ("JAVA_HOME is not set! Exiting");
exit (1);
} else {
int length = strlen (java_home) + strlen (java_bin_affix) + 1;
char * java_bin = (char *) malloc (length);
memset (java_bin, 0, length);
strcat (java_bin, java_home);
strcat (java_bin, java_bin_affix);
execv (java_bin, argv);
}
}
')
This code allows you to replace the process name "java" with your own, by passing the current process to the replacement argv. All arguments will be passed along the JVM chain.
execv, like the rest of the exec family functions, will overwrite the image of the current process, and in such a way that the pid of the starting process and the replacement process will coincide.
After launching a Java application through such a wrapper, the list of processes will look like this:
# ps ax | grep my_daemon
27061 pts / 3 Sl + 0:00 ./my_daemon test
This output is much more usable than multiple “java”.