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

I'm not a shell script expert. I'm looking for a shell script which checks for maximum number of Java processes (MyJavaStandalone) running in the system before starting/executing the current Java process.


Script: /home/myfolder/ contains

/usr/java/jdk1.6.0/bin/java MyJavaStandalone >> $DATE.log &

Here, before executing "MyJavaStandalone", if there are already 10 processes running, then this current process should not be started.

share|improve this question
up vote 2 down vote accepted

Try something like this, assuming you have the 'procps' tools installed:


if (( $(pgrep -cf "java MyJavaStandalone") < 10 ))
    /usr/java/jdk1.6.0/bin/java MyJavaStandalone >> $DATE.log &


The pgrep command counts the number of copies of your script currently in the process table. The (( ... )) construct is a simple way to do basic math comparisons and operations in the shell, in this case checking to see if the output of the pgrep command is less than 10.

share|improve this answer
@Insyte First, thank you for the script. FYI. -c is an invalid option. pgrep: invalid option -- c – Gnanam Mar 27 '10 at 10:04
It must depend on the version; it works on my systems. You can use pgrep -f java MyJavaStandalone|wc -l as a replacement. I'm running version 3.2.7, by the way. – Insyte Mar 27 '10 at 20:23

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.