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 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
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.

java &
sleep 10
if kill -0 $PID; then
    echo "running"
    echo "not running"
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:

if ($(ps aux | grep $MYPROCESS | grep -v “grep $MYPROCESS″ | wc -l) > 0)
echo "$MYPROCESS service running, everything is fine" >
echo "$MYPROCESS is not running"

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, 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

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


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.