Java Execute Bash Script that runs daemonized script

I am working on a bash script that runs when a My Java application application requests a server reboot. This script performs operations that must be outside the Java application process tree.

I invoke a restart of the script in Java using ProcessBuilder as follows:

// 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 performs the dedesignation of another script that processes all the reload logic. It looks like this:

#!/bin/bash
(bash /some/directory/shutdownHandler.sh "true" &)
exit 0

When I call a function containing the ProcessBuilder logic in a Java application, I don’t see the effects of the logic in the shutdownHandler.sh script. Even simple echoes of text in files do not arise. I have already verified that I have the correct permissions.

restart.sh , , .

, . - , Java script?

+4
2

script shutdownHandler.sh , . , shutdownHandler.sh. restart.sh :

#!/bin/bash
LOGFILE="/some/log/directory/scriptLog.log"
(setsid /some/directory/shutdownHandler.sh "true" >$LOGFILE 2>&1 < /dev/null &)
exit 0

:

  • stdout, stderr stdin script. stdout stderr LOGFILE dev/null stdin
  • tty
  • script init. shutdownHandler.sh , JVM
0

:

final String[] RESTART_COMMAND = { "nohup", "/some/directory/shutdownHandler.sh", "true" };
final ProcessBuilder pb = new ProcessBuilder(RESTART_COMMAND);
Process p = pb.start();
//DON'T waitFor()

nohup waitFor , RESTART_SCRIPT: (jvm shutdownHandler.sh, Java shutdownHandler.sh).

0

Source: https://habr.com/ru/post/1685190/


All Articles