I have created a linux auto-update bash script that is called from my java app using Runtime.getRuntime().exec() whenever the command 'sudo yum check-update' returns a positive result.
This bash script always starts the update process by stopping the systemd service that runs the java app jar file. It does this with sudo service myApp stop. The problem i'm facing is that when the java app terminates, the process in which the bash script is running is terminated as well. So the bash script never gets past the service stop command.
I know that when the parent process in which the java program runs terminates, any child processes that were spawned by the parent process will terminate as well. So to prevent this i've tried turning the bash script execution into a daemon thread that should continue even after the java program has shut down. The code looks like this:
new Thread(() -> {
try {
String[] command = new String[]{"env", "bash", "-c", "setsid nohup " + String.format("/home/ec2-user/config/linux-update-script.sh" + " %s %s", LinuxUpdateTask.SYSTEM_UPGRADE, "2023.0.20230503") + " >/dev/null 2>&1 </dev/null &"};
Process process = new ProcessBuilder(command).start();
process.waitFor();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}).start();
This command calls setsid and nohup which should detach the process from the parent process that spawned it. I'm also running the command in a separate thread. But despite using setsid the process is still killed when the java program is terminated.
I've solved this problem by running the update script via a systemd service. The java program calls this service which in turn calls the bash file and the java program terminates without terminating the bash file process.
But i want to know if there is any way i could do this without having to use a systemd service. Are there any JVM settings that i could enable or disable to prevent the daemon process from being terminated when the spawning java program terminates?