Solved

how to check the status of my java application

Posted on 2016-07-14
8
99 Views
Last Modified: 2016-07-27
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
Comment
Question by:Jason Yu
  • 4
  • 4
8 Comments
 
LVL 77

Accepted Solution

by:
arnold earned 500 total points
ID: 41711656
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
 

Author Comment

by:Jason Yu
ID: 41728264
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
 

Author Comment

by:Jason Yu
ID: 41728267
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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 77

Expert Comment

by:arnold
ID: 41728777
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
 

Author Comment

by:Jason Yu
ID: 41731626
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
 
LVL 77

Expert Comment

by:arnold
ID: 41731684
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
 

Author Comment

by:Jason Yu
ID: 41731800
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
 
LVL 77

Expert Comment

by:arnold
ID: 41732241
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
bash file 10 49
Run Secure WMI query from CentOS 5 82
Intel fortran compiler (ifort) 5 38
Need to combine two scripts 2 33
Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question