Check if jar running from shell

南笙酒味 提交于 2019-12-07 17:33:42

问题


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:


You could use the jps command. It return the JVMs running in the system.




回答6:


I created following script to monitor my application jar is running or not. In this case My application jar is running on port 8080

#!/bin/bash
check=$(netstat -an | grep 8080 | wc -l)
if [[ $check -eq 0 ]];then
    echo "jar is not running..."
    /usr/bin/java -jar /path/to/target/application.jar >> /dev/null &
else
    echo "it is running"
fi

I am using cronjob to monitor jar app by executing shell script on every minute.

$ crontab -e

in the end of file

* * * * * /bin/bash monitor-jar.sh /dev/null 2>&1



回答7:


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



来源:https://stackoverflow.com/questions/20168332/check-if-jar-running-from-shell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!