1

I am developing a Java program which checks the running processes, if not start that process. In my context I am executing the .sh file which is like this.

#!/bin/sh
echo "Hello World..."
cnt=`ps -eaflc --sort stime | grep clientApplication.jar | grep -v grep | wc -l`
if [ $cnt = 3 ] 
then
        echo "Services for Pigeon are already running..."
else
        echo "Starting Services for Pigeon..."
        echo `java -jar clientApplication.jar`
fi

But it's not working. What's the problem?

Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80
Suniel
  • 1,449
  • 8
  • 27
  • 39

4 Answers4

0

I am not sure. Try this

cnt=$(`ps -eaflc --sort stime | grep clientApplication.jar | grep -v grep | wc -l`)
if[$cnt -eq 3]; then

Just try this

sujeesh
  • 488
  • 2
  • 7
  • 15
0

Using the test expression works. Hope this helps !!

echo "Hello World"
cnt=`ps -eaflc --sort stime | grep clientApplication.jar |grep -v grep | wc -l`
if(test $cnt -eq 3) ;
then
    echo "Services for Pigeon are already running..."
else
    echo "Starting Services for Pigeon..."
    echo `java -jar clientApplication.jar`
fi
Anugoonj
  • 575
  • 3
  • 9
0

echo won't execute anything it will just print the content what is there in double quotes. So if i have understood your requirement you should be doing this instead of

echo "Hello World" cnt=ps -eaflc --sort stime | grep clientApplication.jar |grep -v grep | wc -l if(test $cnt -eq 3) ; then echo "Services for Pigeon are already running..." else echo "Starting Services for Pigeon..." /bin/java -jar clientApplication.jar fi

Hope this will work, i haven't tested it now
ASHU
  • 214
  • 2
  • 5
0
  1. Equality checks should be ==, = is assignment
  2. I'd use $(expr) rather than `expr`
  3. if(expr) not if[expr]
  4. not sure if you intend to echo the java line or not, it's not going to start the jar by telling it to print. I got rid of the echo since you said you wanted to start it.

so if you try this, it will work:

    #!/bin/sh
    echo "Hello World..."
    cnt=$(ps -eaflc --sort stime | grep clientApplication.jar | grep -v grep | wc -l)
    if($cnt==3) 
    then
        echo "Services for Pigeon are already running..."
    else
        echo "Starting Services for Pigeon..."
        java -jar clientApplication.jar
    fi
anNA
  • 330
  • 1
  • 2
  • 9