开发者

restart java app in ubuntu

开发者 https://www.devze.com 2023-02-01 10:16 出处:网络
What\'s the best way to restart a java app in ubuntu?I know u can run commands like this one from the terminal, but it doesnt seem to be working...

What's the best way to restart a java app in ubuntu? I know u can run commands like this one from the terminal, but it doesnt seem to be working...

      开发者_运维百科  String restartArgs = "java -jar \'/home/fdqadmin/NetBeansProjects/dbConvert2/dist/dbConvert2.jar\' --Terminal=true";
        Process restart = Runtime.getRuntime().exec(restartArgs);
        System.exit(1);


You are killing the parent process with System.exit(1), so its child process is destroyed as well. To restart you would typically provide a shell script wrapper to launch the actual Java app.

#!/bin/sh
restartCode="1"; # predefined restart signal code
java -jar '/home/fdqadmin/NetBeansProjects/dbConvert2/dist/dbConvert2.jar' --Terminal=true; # run java program
if [ $? -eq restartCode ] # if exit code is equal to predefined restart signal code
then
  $0; # restart script
fi
exit $?;

Note the above code is a rough, crude outline. Typical wrappers are far more complex to deal with commandline arguments passed to the startup script itself etc. etc. Plus, my sh-skills are not infallible.


try providing full path for JAVA_HOME (e.g /usr/lib/jvm/java-6-sun/bin/java instead of java). The exec does not have Shell enironment variables.

also use

 restart.waitFor(); //wait until process finishes 

to make sure Java does not exit before the process finishes.


If you do want to run in shell (and use shell specific stuffs like pipe and ls) do this:

List<String> commands = new ArrayList<String>();
commands.add("/bin/sh");
commands.add("-c");
commands.add("java -jar /home/fdqadmin/NetBeansProjects/dbConvert2/dist/dbConvert2.jar");

SystemCommandExecutor commandExecutor = new SystemCommandExecutor(commands);
int result = commandExecutor.executeCommand();
commandExecutor.waitFor(); //wait until process finishes
0

精彩评论

暂无评论...
验证码 换一张
取 消