Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question

3 Answers 3

up vote 1 down vote accepted

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.

share|improve this answer
    
This might work - thank you. –  Timur Fanshteyn Jul 14 '09 at 14:23

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.

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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