What is the best way to run multiple Almost Identical Java processes on the same Linux server?

Explanation of the environment:

We are running a set of Java processes that provide a calculation grid. The only difference between the processes, is the command line parameter that Identifies the node name. Something like:

(Process 1) Java -DNodeNumber=1 CalculationNode

(Process 2) Java -DNodeNumber=2 CalculationNode

(Process 3) Java -DNodeNumber=3 CalculationNode

(Process 4) Java -DNodeNumber=4 CalculationNode

The script that starts up a each process is relatively simple, but not trivial since there are around 15 other parameters that are needed - THEY ARE IDENTICAL FOR ALL PROCESSES

I need to be able to restart a process if it fails with the SAME NodeNumber parameter. In other words - if Node 3 fails, I need to restart it as Node 3.


I'd argue that if a process fails, you need to fix it in order to not fail anymore.

To do what you expect, something like this would suffice (untested, use at your own risk):

for i in $(seq 1 4)
    echo "Starting node $i..."
    while ! java -DNodeNumber=$i CalculationNode
      sleep 1
      echo "Restarting node $i..."
  ) &

Each process has to finish with exit code zero in order to break the loop. Otherwise, it is restarted by the script.


Juliano's solution might work in simple cases, but it doesn't cover every situation. For example, one of your processes could exit with exit status 0 even though an error occured, if there is an error in the error handling code. Or it could deadlock or run into some other kind of infinite loop without doing anything useful.

So if you want a fancier solution, try Nagios. It allows you to write plugins for special monitoring tasks.


You could always use daemontools to monitor and restart your processes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.