Check if jar running from shell
Asked Answered
M

7

7

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.

Mcquoid answered 23/11, 2013 at 21:47 Comment(2)
What do you mean by it does not appear to be working? What exactly is the problem?Masorete
It always goes to starting service . It can't detect if the program is already runningMcquoid
T
4

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.

Technician answered 25/11, 2013 at 16:23 Comment(0)
I
4

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.

Inaugurate answered 25/11, 2013 at 15:47 Comment(1)
Storing the pid in a file is not very clean. Some other process could alter the file, the file could get corrupt or maybe the process even gets a new pid. There are a lot of things that can go wrong with this, so i really advice to use other solutionsRudderhead
T
4

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.

Technician answered 25/11, 2013 at 16:23 Comment(0)
D
1

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

Discretion answered 25/11, 2013 at 15:32 Comment(0)
L
1

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
Limestone answered 30/12, 2015 at 8:8 Comment(0)
M
0

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

Masorete answered 23/11, 2013 at 23:49 Comment(3)
When doing a pgrep java it came up with 3 processes runningMcquoid
The trouble looks to be coming from: test: -eq: unary operator expectedMcquoid
But pgrep java is not what your command actually does! You are searching for a specific application! I would do it as if [ $cnt -eq 1 ]. You need to experiment with bits of your script until they work right, not just run your script and pray.Masorete
F
0

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

Faubion answered 13/9, 2019 at 11:13 Comment(0)
G
0

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
Guideline answered 13/9, 2019 at 12:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.