Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I would like to start a java process...and then check if its still running after 10 seconds from a bash script.

Is there an easy way to do this?

share|improve this question

5 Answers 5

up vote 3 down vote accepted

You can do this a bit better by using $! to get the PID of the last executed command, and using kill -0 to check if it's still running.

#!/bin/sh
java &
PID=$!
sleep 10
if kill -0 $PID; then
    echo "running"
else
    echo "not running"
fi
share|improve this answer

Thanks for your question. You might want to take a look at the following sample script and see if this is what you require:

#!/bin/sh
MYPROCESS='your-java-process'
if ($(ps aux | grep $MYPROCESS | grep -v “grep $MYPROCESS″ | wc -l) > 0)
then
echo "$MYPROCESS service running, everything is fine" >
else
echo "$MYPROCESS is not running"
fi

The ‘if’ statement below checks to see if the process is running with the help of the ‘ps’ command. In the if statement, "ps aux" lists the current running processes. The next section finds out the your process, then we eliminate the grep process from the obtained list of your processes and finally, count the number of instances. If that result is greater than zero, your process is running, else it isn't.

Hope this helps...

share|improve this answer

Im suprised no one mentioned "jps". Try it!

share|improve this answer

well, make a file called something.sh, replace program with the name of your program:

java -jar program
sleep 10
ps aux| grep program

If the output is empty then it has stopped working. Launch it with sh something.sh.

share|improve this answer

Save the Process ID of the java process you created, (invoke it immediately).

PID=$(pgrep -n java | head -n 1)

Later use process search to find using the PID

ps --no-headers $PID

at this point if $? is 0, then the process is still running. If it is 1, the process exited.

share|improve this answer

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.