Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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.

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


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.