0

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.

pQd
  • 29,561
  • 5
  • 64
  • 106

3 Answers3

2

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):

#!/bin/bash
for i in $(seq 1 4)
do
  (
    echo "Starting node $i..."
    while ! java -DNodeNumber=$i CalculationNode
    do
      sleep 1
      echo "Restarting node $i..."
    done
  ) &
done
wait

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

Juliano
  • 5,402
  • 27
  • 28
0

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.

Kim
  • 729
  • 1
  • 5
  • 12
0

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

Philip Wigg
  • 104
  • 4