1

I need to create a SSH BASH script (on Debian linux) to test if 'java' process is running.

Here how it should look like:

IF 'java' process is not running  THEN run ./start.sh

to test if java process is running, I can make this test:

ps -A | grep java

This script should run every minute (I guess in a CRON)

Regards

0

3 Answers 3

2

First of all, to run a job every minute in cron, your crontab should look like this:

* * * * * /path/to/script.sh

Next, you have a few different options for detecting a Java process.

Note that each of the following is a negation: they detect the absence of Java:

  • With pgrep:

    if [ ! $(pgrep java) ] ; then
      # no java running
    fi
    
  • With pidof:

    if [ ! $(pidof java) ] ; then
      # no java running
    fi
    
  • With ps and grep:

    if [ ! $(ps -A | grep 'java') ] ; then
      # no java running
    fi
    

Of these, pgrep and pidofare probably the most efficient. Don't quote me on that, though.

Sign up to request clarification or add additional context in comments.

Comments

1

The check you are doing with PS and GREP doesn't look very detailed. What if other Java processes are running ? You may detect those, and come to a wrong conclusion, because you are just checking "any" Java, not some specific Java.

Comments

0

With pidof would be something like this:

script.sh

pidof java;
if[$? -ne 0];
then
   # here put your code when errorcode of `pidof` wasn't 0, means that it didn't find process
   # for example: /home/user/start.sh 
   # (please don't forget to use full paths if you want to use it in cron)
fi

Especially for haters:

man pidof:

EXIT STATUS

   0      At least one program was found with the requested name.
   1      No program was found with the requested name.

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.