Solved

how to check the status of my java application

Posted on 2016-07-14
8
70 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 76

Accepted Solution

by:
arnold earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 76

Expert Comment

by:arnold
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:Jason Yu
Comment Utility
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 76

Expert Comment

by:arnold
Comment Utility
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
Comment Utility
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 76

Expert Comment

by:arnold
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now