Check if jar running from shell

2019-05-16 16:49发布

问题:

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.

回答1:

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.



回答2:

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.



回答3:

I did the very same scenario a couple of months ago. My task was to ensure a jar distributed java program to run 24/7 on a Linux server. My program was console-based, started, did something then stopped. I did a shell script that started, waited to end and then re-started the app in an infinite loop. I installed runit, created a service and supplied this script as the run script. Works very well. In general, the shell script ensures that the java program is running and runit ensures that the start script (which is our script) is running.

You find valuable info here: http://smarden.org/runit/faq.html



回答4:

Rather than putting the process to sleep , I'd rather have it exit and use crontab to run the process every 1 min;which will check if its running or else just stop the script.

#!/bin/sh
declare -a devId=( "/Path/To/TestJar.jar Test1" "/Path/To/TestJar.jar Test2" ) #jarfile with pathname and Test as argument
# get length of an array
arraylength=${#devId[@]}
# use for loop to read all values and indexes
for (( i=1; i<${arraylength}+1; i++ ));
do
y=${devId[$i-1]}
cnt=`ps -eaflc --sort stime | grep "$y" |grep -v grep | wc -l`
if [ $cnt = 0 ]
then
java -jar $y& > /dev/null
b=$(basename $y)
echo $b 
#DO SOME OPERATION LIKE SEND AN EMAIL OR ADD TO LOG FILE
continue
elif [ $cnt != 0 ]
then
echo 'do nothing'
fi
done


回答5:

Why do you think $cnt should be equal to 3? Shouldn't it be equal to 1 if the process is already running?