Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 178
  • Last Modified:

how to check the status of my java application

I have the following service wrapper for a java application. My coworker wants me to expand it to include "service XXX status" and echo back the current status. How could I add status check on this script?

#!/bin/sh
SERVICE_NAME=pms
PATH_TO_JAR=/data/servicename.jar
PID_PATH_NAME=/tmp/servicename-pid
cd /data
case $1 in
    start)
        echo "Starting $SERVICE_NAME ..."
        if [ ! -f $PID_PATH_NAME ]; then
            nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
                        echo $! > $PID_PATH_NAME
            echo "$SERVICE_NAME started ..."
        else
            echo "$SERVICE_NAME is already running ..."
        fi
    ;;
    stop)
        if [ -f $PID_PATH_NAME ]; then
            PID=$(cat $PID_PATH_NAME);
            echo "$SERVICE_NAME stoping ..."
            kill $PID;
            echo "$SERVICE_NAME stopped ..."
            rm $PID_PATH_NAME
        else
            echo "$SERVICE_NAME is not running ..."
        fi
    ;;
    restart)
        if [ -f $PID_PATH_NAME ]; then
            PID=$(cat $PID_PATH_NAME);
            echo "$SERVICE_NAME stopping ...";
            kill $PID;
            echo "$SERVICE_NAME stopped ...";
            rm $PID_PATH_NAME
            echo "$SERVICE_NAME starting ..."
            nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
                        echo $! > $PID_PATH_NAME
            echo "$SERVICE_NAME started ..."
        else
            echo "$SERVICE_NAME is not running ..."
        fi
    ;;
esac
0
Jason Yu
Asked:
Jason Yu
  • 4
  • 4
1 Solution
 
arnoldCommented:
Add a check on whether the current PID that you wrote out, $PID_PATH_NAME, is running and is correct, I.e. Running the Java app.
Another way is to use ps -ef | grep Java |grep -v and check whether the response matches the PID you stored when the spa started.
Does the Java app provide for a way to check whether it is listening/responding?
0
 
Jason YuAuthor Commented:
Hi, Arnold, thanks for your reply, I was busy with another project last week and didn't update on time.

Now I have to work on this again.

I modified the script by importing a system function @/etc/init.d/functions, and added status check through calling the "status function" defined in this system function(/etc/init.d/functions). However, it couldn't check the status correctly, it always shows "pms dead but subsys locked" (pms is the service/java application name). Do I need change my script to check the pid file as you suggested?

I am following this article to do the java application status check : "http://www.cyberciti.biz/tips/linux-write-sys-v-init-script-to-start-stop-service.html".


#!/bin/sh
#
# chkconfig: 35 90 12
# description: Foo server
#
# Get function from functions library
. /etc/init.d/functions
# Start the service pms
SERVICE_NAME=pms
PATH_TO_JAR=/data/AveryRollProduction.jar
JVM_ARGS="-Dspring.profiles.active=dev"
PID_PATH_NAME=/tmp/pmsService-pid
cd /data
case $1 in
    start)
        echo "Starting $SERVICE_NAME ..."
        if [ ! -f $PID_PATH_NAME ]; then
            nohup java $JVM_ARGS -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
                        echo $! > $PID_PATH_NAME
### Create the lock file ###
        touch /var/lock/subsys/pms
            echo "$SERVICE_NAME started ..."
        else
            echo "$SERVICE_NAME is already running ..."
        fi
    ;;
    stop)
        if [ -f $PID_PATH_NAME ]; then
            PID=$(cat $PID_PATH_NAME);
            echo "$SERVICE_NAME stoping ..."
            kill $PID;
### Now, delete the lock file ###
        rm -f /var/lock/subsys/pms
            echo "$SERVICE_NAME stopped ..."
            rm $PID_PATH_NAME
        else
            echo "$SERVICE_NAME is not running ..."
        fi
    ;;
    status)
            status pms
    ;;

    restart)
        if [ -f $PID_PATH_NAME ]; then
            PID=$(cat $PID_PATH_NAME);
            echo "$SERVICE_NAME stopping ...";
            kill $PID;
            echo "$SERVICE_NAME stopped ...";
            rm $PID_PATH_NAME
            echo "$SERVICE_NAME starting ..."
            nohup java $JVM_ARGS -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
                        echo $! > $PID_PATH_NAME
            echo "$SERVICE_NAME started ..."
        else
            echo "$SERVICE_NAME is not running ..."
        fi
    ;;
esac

Open in new window

0
 
Jason YuAuthor Commented:
Basically, I followed that article by importing the system function @/etc/init.d/functions

And then created a lock file when I start the application, deleted it when I stop. When I need check the status of the application, the script calls the "status" function defined inside the system function.

The result is when I check the status, it always shows

[root@pmsdev init.d]# service pms status
pms dead but subsys locked
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
arnoldCommented:
The command status does not work.
The Java app is not name Pms so the status function will not find it in the process list, but since Pms is used for the lock filename. Which is matched and this is why you get the response you did.
I.e, ps -ef | grep pms finds no matches
Ls -l /var/lock/subsystem/pms matches, which is why you get the lock is in place message...
0
 
Jason YuAuthor Commented:
I got it now. I used the following script to check the pid file to decide if the service is running. if the pid file exist, then it's running, if now, the service is not running, thank you very much.

status)
         if [ -e $PID_PATH_NAME ]; then
            echo "$SERVICE_NAME pid file exist, the $SERVICE_NAME service is running"

        else
            echo "$SERVICE_NAME pid file doesn't exist, the $SERVICE_NAME service is not running ..."
        fi
    ;;
0
 
arnoldCommented:
You should add another check to make sure the PID from the PID file is actually in the process table and is the process/application you expect it to be..
0
 
Jason YuAuthor Commented:
how could I do this, can I compare the PID number saved in this file with the result of "ps -ef | grep java" command. But "ps -ef | grep java"  always gives me two lines of results.

[root@pmsdev init.d]# ps -ef | grep java
root     31395     1  3 10:32 pts/1    00:02:00 java -Dspring.profiles.active=dev -jar /data/AveryRollProduction.jar /tmp
root     31727 30119  0 11:39 pts/0    00:00:00 grep --color=auto java
[root@pmsdev init.d]# service pms status
pms pid file exist, the pms service is running
The pid number is 31395
[root@pmsdev init.d]#

Open in new window

0
 
arnoldCommented:
Yes,
You can get the PID
Then ps -ef | grep $pid| grep Java |grep -v grep

If you get a result from the above ...
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now