1

Following is my script, every time I run this it goes into else part. when I run the TEST2EVAL command it gives me 1

#!/bin/sh
TEST2EVAL='ps auxf | grep some.jar | grep -v grep | wc -l'
if [ "$TEST2EVAL" = 1 ]
then
java -jar /path/to/jar &
else
echo "Running"
fi

3 Answers 3

3

Assuming you are trying to find out if any processes are running with some.jar on their command lines you probably want:

if pgrep -f some.jar; then
    echo running;
else
    echo not running;
fi
Sign up to request clarification or add additional context in comments.

3 Comments

yes, trying to find a process, and if its not running, would like to start it! its working now!
@sssV It should probably be pointed out that this isn't testing for a java process using that jar. Anything touching that jar (or any other file with the same name) will trip that "already running" detection.
yeah, will use unique name and will be using this script for cronjob
1

In in order save the output of a command in a variable, you have to enclose the command in backticks (`), not single quotes ('). Thus, change the second line of your script to:

TEST2EVAL=`ps auxf | grep some.jar | grep -v grep | wc -l`

Comments

1

You are using the wrong quotes for command substitution: not single quotes:

TEST2EVAL='ps auxf | grep some.jar | grep -v grep | wc -l'

but backquotes:

TEST2EVAL=`ps auxf | grep some.jar | grep -v grep | wc -l`

Better yet, use TEST2EVAL=$(ps auxf | grep some.jar | grep -v grep | wc -l) instead. It's much clearer, supported by all POSIX-compatible shells, and can be nested more easily when necessary.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.