Java executes bash scripts that run daemon scripts
I'm processing a bash script that runs when my java server application requests a server restart The actions performed by this script need to be outside the process tree of the Java application
I use processbuilder to call the restart script in Java in the following ways:
// Vars declared at the top of the file private static final String LOC = "/some/directory/"; private static final String RESTART_SCRIPT = LOC + "restart.sh"; ... // In the function that is invoked to handle reboot behavior final ProcessBuilder pb = new ProcessBuilder(RESTART_SCRIPT); Process p = pb.start();
This script does the following to deaminize another script that handles all restart logic It looks like this:
#!/bin/bash (bash /some/directory/shutdownHandler.sh "true" &) exit 0
When I called functions containing ProcessBuilder logic in Java Application, I was not in shutdownHandler.. See the impact of logic in the SH script Even simple text - to - file echoes do not occur I've checked that I have the right permissions
When I execute restart directly from the command line SH, it works as expected
Please tell me why I see this difference in behavior Does java have scripts that kill daemons?
Solution
I suggest you try the following simplification:
final String[] RESTART_COMMAND = { "nohup","/some/directory/shutdownHandler.sh","true" }; final ProcessBuilder pb = new ProcessBuilder(RESTART_COMMAND); Process p = pb.start(); //DON'T waitFor()
Using nohup and avoiding waitfor should have restart_ Script achieves the same effect: the life cycles of the two processes are independent (the JVM does not wait for shutdownhandler.sh, and the termination of Java processes will not cause the interruption of shutdownhandler. SH)