Check if jar running from shell

Store your process id in file and check for this process.

#!/bin/bash

while [ true ]
do
    pid=$(cat /tmp/portal.pid)
    if [[ -n "$pid" && $(ps -p $pid | wc -l) -eq 2 ]]
    then
        echo "Service already running..."
    else
        echo "Starting Service"
        java -jar /home/Portal.jar >> /dev/null &
        echo $! > /tmp/portal.pid
    fi
    sleep 1m
done

/tmp will be cleared on restart, all right in this case.


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.