Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if jar running from shell

I have a java jar program that I am trying to run on startup of my machine. Ideally, the shells script will check every 60 seconds to assure that the jar is running. How do I check if the jar is running on centos, this does not appear to be working?

My current .sh file:

#!/bin/bash

while [ true ]
do
    cnt=`ps -eaflc --sort stime | grep /home/Portal.jar |grep -v grep | wc -l`
    if(test $cnt -eq 3);
    then
        echo "Service already running..."
    else
        echo "Starting Service"
        java -jar /home/Portal.jar >> /dev/null &
    fi
    sleep 1m
done

I used this for referencing so far.

like image 886
Sixthpoint Avatar asked Nov 23 '13 21:11

Sixthpoint


1 Answers

Depending on what your program does, there may be more or less intelligent ways to check it. For example, if you have some server, it will listen on a port.

Then something like

netstat -an | fgrep tcp | fgrep LISTEN | fgrep :87654   # or whatever your port is

could do the job.

Then there is lsof, which could also detect listening ports.

Finally, you could connect and issue a pseudo request. For example, for a http server, you could use lynx or curl. For a server with a non-stamdard protocol, you can write a small client program whose sole purpose is to connect to the server just to see if it is there.

like image 152
Ingo Avatar answered Sep 29 '22 09:09

Ingo