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.

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.

Example:

Script: /home/myfolder/script.sh

script.sh 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
add comment

1 Answer

up vote 2 down vote accepted

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

#!/bin/bash

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

Explanation:

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
add comment

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.