1

i usually use this bash script to check if my java application is running and start it again if not. I use crontab to check it

#!/bin/bash
if [ "$(pidof java)" ]
then
  # process was found
        echo "application running"
else
  # process not found
cd /home/assist/emanager
setsid java -jar emanager-1.0.0.jar </dev/zero &>/dev/null &
fi

Now the problem is that there are other java applications running on the server, so the script does not start my app because the if condition is true. Is there a way to check if a specific java application is running?

Thanks

MarioC
  • 2,934
  • 15
  • 59
  • 111
  • 3
    grep for `emanager-1.0.0.jar` in the process list? – arco444 Jan 31 '17 at 09:29
  • Writing a PID file when emanager application start, unlink it at exit. This may be done by the launcher script or by the application itself. – Aubin Jan 31 '17 at 09:30
  • You could also create a 'lock' file when the application starts and delete it when it exits, then check if the 'lock' file exists. – Philip Rollins Jan 31 '17 at 09:31
  • so, I should use a script to automatically start the application at startup, write the pid on a file or create a lock file, and check if it exists... – MarioC Jan 31 '17 at 09:33
  • what would be the workflow if you add the restart kind of script to restart the jar once again even if its already running ? – thar45 Jan 31 '17 at 09:59

1 Answers1

0

Your question is very similar to this one: Start a Java application using Bat file if not already started

The main difference is you are asking for bash while the other one is in Windows. Hence replace jcmd with jps, the rest is still applicable.

Queeg
  • 7,748
  • 1
  • 16
  • 42